Skip to content

Commit f3bfe27

Browse files
Dennis-Mirceasbrannen
authored andcommitted
Document @⁠Conditional gating of nested @⁠Configuration classes
Closes gh-36831 Signed-off-by: Dennis-Mircea Ciupitu <dennis.mircea.ciupitu@gmail.com>
1 parent 7651d58 commit f3bfe27

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

framework-docs/modules/ROOT/pages/core/beans/java/composing-configuration-classes.adoc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,19 @@ Kotlin::
610610
See the {spring-framework-api}/context/annotation/Conditional.html[`@Conditional`]
611611
javadoc for more detail.
612612

613+
[NOTE]
614+
====
615+
A `@Conditional` declared on an enclosing `@Configuration` class gates the
616+
registration of nested `@Configuration` classes within it only when the
617+
nested class is reached through the parser's recursion from its enclosing
618+
class, or through `@Import`. When the nested class is discovered
619+
independently of its enclosing class, for example via `@ComponentScan` or
620+
by directly registering it against the application context, it is processed
621+
using only its own `@Conditional` annotations. In that case, redeclare the
622+
relevant conditions on the nested class, or extract them into a composed
623+
annotation applied to both, if the same gating is intended.
624+
====
625+
613626

614627
[[beans-java-combining]]
615628
== Combining Java and XML Configuration

spring-context/src/main/java/org/springframework/context/annotation/Configuration.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,18 @@
341341
* with the {@code @Profile} annotation to provide two options of the same bean to the
342342
* enclosing {@code @Configuration} class.
343343
*
344+
* <p>{@link Conditional @Conditional} annotations declared on an enclosing
345+
* {@code @Configuration} class gate registration of any nested
346+
* {@code @Configuration} classes within it when the nested class is reached
347+
* through the parser's recursion from its enclosing class (the case shown
348+
* above) or via {@link Import @Import}. When the nested class is discovered
349+
* independently of its enclosing class, for example via
350+
* {@link ComponentScan @ComponentScan} or by directly registering the nested
351+
* class against the application context, it is processed using only its own
352+
* {@code @Conditional} annotations. In that case, redeclare the relevant
353+
* conditions on the nested class, or extract them into a composed
354+
* annotation applied to both, if the same gating is intended.
355+
*
344356
* <h2>Configuring lazy initialization</h2>
345357
*
346358
* <p>By default, {@code @Bean} methods will be <em>eagerly instantiated</em> at container

0 commit comments

Comments
 (0)