From 2f8c5a580ae34d7504f20cd7c4a6f92399bab393 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Deleuze?= Date: Wed, 19 Mar 2025 15:11:43 +0100 Subject: [PATCH] Polishing --- .../beans/factory/BeanRegistry.java | 13 ++-- .../beans/factory/BeanRegistrarDsl.kt | 68 ++++++++++--------- 2 files changed, 42 insertions(+), 39 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/BeanRegistry.java b/spring-beans/src/main/java/org/springframework/beans/factory/BeanRegistry.java index 7a53d29eca3..5a81d6f8e63 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/BeanRegistry.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/BeanRegistry.java @@ -38,6 +38,13 @@ import org.springframework.core.env.Environment; */ public interface BeanRegistry { + /** + * Register beans using the given {@link BeanRegistrar}. + * @param registrar the bean registrar that will be called to register + * additional beans + */ + void register(BeanRegistrar registrar); + /** * Given a name, register an alias for it. * @param name the canonical name @@ -88,12 +95,6 @@ public interface BeanRegistry { */ void registerBean(String name, Class beanClass, Consumer> customizer); - /** - * Register beans using the given {@link BeanRegistrar}. - * @param registrar the bean registrar that will be called to register - * additional beans - */ - void register(BeanRegistrar registrar); /** * Specification for customizing a bean. diff --git a/spring-beans/src/main/kotlin/org/springframework/beans/factory/BeanRegistrarDsl.kt b/spring-beans/src/main/kotlin/org/springframework/beans/factory/BeanRegistrarDsl.kt index 9aa38e8b2ae..8daf689cf4b 100644 --- a/spring-beans/src/main/kotlin/org/springframework/beans/factory/BeanRegistrarDsl.kt +++ b/spring-beans/src/main/kotlin/org/springframework/beans/factory/BeanRegistrarDsl.kt @@ -64,6 +64,41 @@ open class BeanRegistrarDsl(private val init: BeanRegistrarDsl.() -> Unit): Bean */ lateinit var env: Environment + + /** + * Apply the nested block if the given profile expression matches the + * active profiles. + * + * A profile expression may contain a simple profile name (for example + * `"production"`) or a compound expression. A compound expression allows + * for more complicated profile logic to be expressed, for example + * `"production & cloud"`. + * + * The following operators are supported in profile expressions: + * - `!` - A logical *NOT* of the profile name or compound expression + * - `&` - A logical *AND* of the profile names or compound expressions + * - `|` - A logical *OR* of the profile names or compound expressions + * + * Please note that the `&` and `|` operators may not be mixed + * without using parentheses. For example, `"a & b | c"` is not a valid + * expression: it must be expressed as `"(a & b) | c"` or `"a & (b | c)"`. + * @param expression the profile expressions to evaluate + */ + fun profile(expression: String, init: BeanRegistrarDsl.() -> Unit) { + if (env.matchesProfiles(expression)) { + init() + } + } + + /** + * Register beans using the given [BeanRegistrar]. + * @param registrar the bean registrar that will be called to register + * additional beans + */ + fun register(registrar: BeanRegistrar) { + return registry.register(registrar) + } + /** * Given a name, register an alias for it. * @param name the canonical name @@ -345,39 +380,6 @@ open class BeanRegistrarDsl(private val init: BeanRegistrarDsl.() -> Unit): Bean return registry.registerBean(T::class.java, customizer) } - /** - * Register beans using the given [BeanRegistrar]. - * @param registrar the bean registrar that will be called to register - * additional beans - */ - fun register(registrar: BeanRegistrar) { - return registry.register(registrar) - } - - /** - * Apply the nested block if the given profile expression matches the - * active profiles. - * - * A profile expression may contain a simple profile name (for example - * `"production"`) or a compound expression. A compound expression allows - * for more complicated profile logic to be expressed, for example - * `"production & cloud"`. - * - * The following operators are supported in profile expressions: - * - `!` - A logical *NOT* of the profile name or compound expression - * - `&` - A logical *AND* of the profile names or compound expressions - * - `|` - A logical *OR* of the profile names or compound expressions - * - * Please note that the `&` and `|` operators may not be mixed - * without using parentheses. For example, `"a & b | c"` is not a valid - * expression: it must be expressed as `"(a & b) | c"` or `"a & (b | c)"`. - * @param expression the profile expressions to evaluate - */ - fun profile(expression: String, init: BeanRegistrarDsl.() -> Unit) { - if (env.matchesProfiles(expression)) { - init() - } - } /** * Context available from the bean instance supplier designed to give access