Skip to content

Commit 87c15ba

Browse files
tgirard12sdeleuze
authored andcommitted
Use the accepted profiles in BeanDefinitionDsl
Closes gh-22393
1 parent ba95818 commit 87c15ba

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

spring-context/src/main/kotlin/org/springframework/context/support/BeanDefinitionDsl.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import org.springframework.beans.factory.getBeanProvider
2323
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils
2424
import org.springframework.context.ApplicationContextInitializer
2525
import org.springframework.core.env.ConfigurableEnvironment
26+
import org.springframework.core.env.Profiles
2627
import java.util.function.Supplier
2728

2829
/**
@@ -1082,11 +1083,12 @@ open class BeanDefinitionDsl(private val init: BeanDefinitionDsl.() -> Unit,
10821083
}
10831084

10841085
/**
1085-
* Take in account bean definitions enclosed in the provided lambda only when the
1086-
* specified profile is active.
1086+
* Take in account bean definitions enclosed in the provided lambda when the
1087+
* profile is accepted.
1088+
* @see org.springframework.core.env.Profiles.of
10871089
*/
10881090
fun profile(profile: String, init: BeanDefinitionDsl.() -> Unit) {
1089-
val beans = BeanDefinitionDsl(init, { it.activeProfiles.contains(profile) })
1091+
val beans = BeanDefinitionDsl(init, { it.acceptsProfiles(Profiles.of(profile)) })
10901092
children.add(beans)
10911093
}
10921094

spring-context/src/test/kotlin/org/springframework/context/support/BeanDefinitionDslTests.kt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,38 @@ class BeanDefinitionDslTests {
170170
context.getBean<Baz>()
171171
}
172172

173+
174+
@Test
175+
fun `Declare beans with accepted profiles`() {
176+
val beans = beans {
177+
profile("foo") { bean<Foo>() }
178+
profile("!bar") { bean<Bar>() }
179+
profile("bar | barbar") { bean<BarBar>() }
180+
profile("baz & buz") { bean<Baz>() }
181+
profile("baz & foo") { bean<FooFoo>() }
182+
}
183+
val context = GenericApplicationContext().apply {
184+
environment.addActiveProfile("barbar")
185+
environment.addActiveProfile("baz")
186+
environment.addActiveProfile("buz")
187+
beans.initialize(this)
188+
refresh()
189+
}
190+
context.getBean<Baz>()
191+
context.getBean<BarBar>()
192+
context.getBean<Bar>()
193+
194+
try {
195+
context.getBean<Foo>()
196+
fail()
197+
} catch (ignored: Exception) {
198+
}
199+
try {
200+
context.getBean<FooFoo>()
201+
fail()
202+
} catch (ignored: Exception) {
203+
}
204+
}
173205
}
174206

175207
class Foo

0 commit comments

Comments
 (0)