diff --git a/spring-context/src/main/kotlin/org/springframework/context/support/BeanDefinitionDsl.kt b/spring-context/src/main/kotlin/org/springframework/context/support/BeanDefinitionDsl.kt index 6cff53c8dd67..29a65df5b8b1 100644 --- a/spring-context/src/main/kotlin/org/springframework/context/support/BeanDefinitionDsl.kt +++ b/spring-context/src/main/kotlin/org/springframework/context/support/BeanDefinitionDsl.kt @@ -23,6 +23,7 @@ import org.springframework.beans.factory.getBeanProvider import org.springframework.beans.factory.support.BeanDefinitionReaderUtils import org.springframework.context.ApplicationContextInitializer import org.springframework.core.env.ConfigurableEnvironment +import org.springframework.core.env.Profiles import java.util.function.Supplier /** @@ -263,11 +264,12 @@ open class BeanDefinitionDsl(private val init: BeanDefinitionDsl.() -> Unit, } /** - * Take in account bean definitions enclosed in the provided lambda only when the - * specified profile is active. + * Take in account bean definitions enclosed in the provided lambda when the + * profile is accepted + * @see org.springframework.core.env.Profiles.of */ fun profile(profile: String, init: BeanDefinitionDsl.() -> Unit) { - val beans = BeanDefinitionDsl(init, { it.activeProfiles.contains(profile) }) + val beans = BeanDefinitionDsl(init, { it.acceptsProfiles(Profiles.of(profile)) }) children.add(beans) } diff --git a/spring-context/src/test/kotlin/org/springframework/context/support/BeanDefinitionDslTests.kt b/spring-context/src/test/kotlin/org/springframework/context/support/BeanDefinitionDslTests.kt index d69e069cd534..25d48c16a240 100644 --- a/spring-context/src/test/kotlin/org/springframework/context/support/BeanDefinitionDslTests.kt +++ b/spring-context/src/test/kotlin/org/springframework/context/support/BeanDefinitionDslTests.kt @@ -156,7 +156,38 @@ class BeanDefinitionDslTests { } context.getBean() } - + + @Test + fun `Declare beans with accepted profiles`() { + val beans = beans { + profile("foo") { bean() } + profile("!bar") { bean() } + profile("bar | barbar") { bean() } + profile("baz & buz") { bean() } + profile("baz & foo") { bean() } + } + val context = GenericApplicationContext().apply { + environment.addActiveProfile("barbar") + environment.addActiveProfile("baz") + environment.addActiveProfile("buz") + beans.initialize(this) + refresh() + } + context.getBean() + context.getBean() + context.getBean() + + try { + context.getBean() + fail() + } catch (ignored: Exception) { + } + try { + context.getBean() + fail() + } catch (ignored: Exception) { + } + } } class Foo