From e38b83c5e50ef8a67b2e06b61f1cf9dfaafba8db Mon Sep 17 00:00:00 2001 From: Dennis-Mircea Ciupitu Date: Sat, 23 May 2026 23:36:32 +0300 Subject: [PATCH 1/2] Document @Conditional gating of nested @Configuration classes Signed-off-by: Dennis-Mircea Ciupitu --- .../beans/java/composing-configuration-classes.adoc | 13 +++++++++++++ .../context/annotation/Configuration.java | 12 ++++++++++++ 2 files changed, 25 insertions(+) diff --git a/framework-docs/modules/ROOT/pages/core/beans/java/composing-configuration-classes.adoc b/framework-docs/modules/ROOT/pages/core/beans/java/composing-configuration-classes.adoc index 89257f072f5f..c7b43b679dd2 100644 --- a/framework-docs/modules/ROOT/pages/core/beans/java/composing-configuration-classes.adoc +++ b/framework-docs/modules/ROOT/pages/core/beans/java/composing-configuration-classes.adoc @@ -610,6 +610,19 @@ Kotlin:: See the {spring-framework-api}/context/annotation/Conditional.html[`@Conditional`] javadoc for more detail. +[NOTE] +==== +A `@Conditional` declared on an enclosing `@Configuration` class gates the +registration of nested `@Configuration` classes within it only when the +nested class is reached through the parser's recursion from its enclosing +class, or through `@Import`. When the nested class is discovered +independently of its enclosing class, for example via `@ComponentScan` or +by directly registering it against the application context, it is processed +using only its own `@Conditional` annotations. In that case, redeclare the +relevant conditions on the nested class, or extract them into a composed +meta-annotation applied to both, if the same gating is intended. +==== + [[beans-java-combining]] == Combining Java and XML Configuration diff --git a/spring-context/src/main/java/org/springframework/context/annotation/Configuration.java b/spring-context/src/main/java/org/springframework/context/annotation/Configuration.java index 95d683ca3597..34127935caad 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/Configuration.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/Configuration.java @@ -341,6 +341,18 @@ * with the {@code @Profile} annotation to provide two options of the same bean to the * enclosing {@code @Configuration} class. * + *

{@link Conditional @Conditional} annotations declared on an enclosing + * {@code @Configuration} class gate registration of any nested + * {@code @Configuration} classes within it when the nested class is reached + * through the parser's recursion from its enclosing class (the case shown + * above) or via {@link Import @Import}. When the nested class is discovered + * independently of its enclosing class, for example via + * {@link ComponentScan @ComponentScan} or by directly registering the nested + * class against the application context, it is processed using only its own + * {@code @Conditional} annotations. In that case, redeclare the relevant + * conditions on the nested class, or extract them into a composed + * meta-annotation applied to both, if the same gating is intended. + * *

Configuring lazy initialization

* *

By default, {@code @Bean} methods will be eagerly instantiated at container From 855ac2c6f3f4a393f560e888544a41c9813b0456 Mon Sep 17 00:00:00 2001 From: Dennis-Mircea Ciupitu Date: Sun, 24 May 2026 21:30:23 +0300 Subject: [PATCH 2/2] Use 'composed annotation' per Spring terminology Signed-off-by: Dennis-Mircea Ciupitu --- .../pages/core/beans/java/composing-configuration-classes.adoc | 2 +- .../org/springframework/context/annotation/Configuration.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/framework-docs/modules/ROOT/pages/core/beans/java/composing-configuration-classes.adoc b/framework-docs/modules/ROOT/pages/core/beans/java/composing-configuration-classes.adoc index c7b43b679dd2..d17e593f24a1 100644 --- a/framework-docs/modules/ROOT/pages/core/beans/java/composing-configuration-classes.adoc +++ b/framework-docs/modules/ROOT/pages/core/beans/java/composing-configuration-classes.adoc @@ -620,7 +620,7 @@ independently of its enclosing class, for example via `@ComponentScan` or by directly registering it against the application context, it is processed using only its own `@Conditional` annotations. In that case, redeclare the relevant conditions on the nested class, or extract them into a composed -meta-annotation applied to both, if the same gating is intended. +annotation applied to both, if the same gating is intended. ==== diff --git a/spring-context/src/main/java/org/springframework/context/annotation/Configuration.java b/spring-context/src/main/java/org/springframework/context/annotation/Configuration.java index 34127935caad..9c0e20fcf40c 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/Configuration.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/Configuration.java @@ -351,7 +351,7 @@ * class against the application context, it is processed using only its own * {@code @Conditional} annotations. In that case, redeclare the relevant * conditions on the nested class, or extract them into a composed - * meta-annotation applied to both, if the same gating is intended. + * annotation applied to both, if the same gating is intended. * *

Configuring lazy initialization

*