Fix Kotlin bean DSL conditional handling
Issue: SPR-16412
This commit is contained in:
@@ -195,7 +195,7 @@ open class BeanDefinitionDsl(private val init: BeanDefinitionDsl.() -> Unit,
|
|||||||
autowireMode: Autowire = Autowire.NO,
|
autowireMode: Autowire = Autowire.NO,
|
||||||
isAutowireCandidate: Boolean? = null,
|
isAutowireCandidate: Boolean? = null,
|
||||||
crossinline function: () -> T) {
|
crossinline function: () -> T) {
|
||||||
|
|
||||||
val customizer = BeanDefinitionCustomizer { bd ->
|
val customizer = BeanDefinitionCustomizer { bd ->
|
||||||
scope?.let { bd.scope = scope.name.toLowerCase() }
|
scope?.let { bd.scope = scope.name.toLowerCase() }
|
||||||
isLazyInit?.let { bd.isLazyInit = isLazyInit }
|
isLazyInit?.let { bd.isLazyInit = isLazyInit }
|
||||||
@@ -255,9 +255,11 @@ open class BeanDefinitionDsl(private val init: BeanDefinitionDsl.() -> Unit,
|
|||||||
*/
|
*/
|
||||||
override fun initialize(context: GenericApplicationContext) {
|
override fun initialize(context: GenericApplicationContext) {
|
||||||
this.context = context
|
this.context = context
|
||||||
for (child in children) {
|
|
||||||
child.initialize(context)
|
|
||||||
}
|
|
||||||
init()
|
init()
|
||||||
|
for (child in children) {
|
||||||
|
if (child.condition.invoke(context.environment)) {
|
||||||
|
child.initialize(context)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,18 +52,21 @@ class BeanDefinitionDslTests {
|
|||||||
@Test
|
@Test
|
||||||
fun `Declare beans using profile condition with the functional Kotlin DSL`() {
|
fun `Declare beans using profile condition with the functional Kotlin DSL`() {
|
||||||
val beans = beans {
|
val beans = beans {
|
||||||
bean<Foo>()
|
profile("foo") {
|
||||||
bean<Bar>("bar")
|
bean<Foo>()
|
||||||
profile("baz") {
|
profile("bar") {
|
||||||
profile("pp") {
|
bean<Bar>("bar")
|
||||||
bean<Foo>()
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
profile("baz") {
|
||||||
bean { Baz(ref()) }
|
bean { Baz(ref()) }
|
||||||
bean { Baz(ref("bar")) }
|
bean { Baz(ref("bar")) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val context = GenericApplicationContext().apply {
|
val context = GenericApplicationContext().apply {
|
||||||
|
environment.addActiveProfile("foo")
|
||||||
|
environment.addActiveProfile("bar")
|
||||||
beans.initialize(this)
|
beans.initialize(this)
|
||||||
refresh()
|
refresh()
|
||||||
}
|
}
|
||||||
@@ -82,7 +85,9 @@ class BeanDefinitionDslTests {
|
|||||||
val beans = beans {
|
val beans = beans {
|
||||||
bean<Foo>()
|
bean<Foo>()
|
||||||
bean<Bar>("bar")
|
bean<Bar>("bar")
|
||||||
bean { FooFoo(env["name"]!!) }
|
environment( { env["name"].equals("foofoo") } ) {
|
||||||
|
bean { FooFoo(env["name"]!!) }
|
||||||
|
}
|
||||||
environment( { activeProfiles.contains("baz") } ) {
|
environment( { activeProfiles.contains("baz") } ) {
|
||||||
bean { Baz(ref()) }
|
bean { Baz(ref()) }
|
||||||
bean { Baz(ref("bar")) }
|
bean { Baz(ref("bar")) }
|
||||||
@@ -97,13 +102,12 @@ class BeanDefinitionDslTests {
|
|||||||
|
|
||||||
assertNotNull(context.getBean<Foo>())
|
assertNotNull(context.getBean<Foo>())
|
||||||
assertNotNull(context.getBean<Bar>("bar"))
|
assertNotNull(context.getBean<Bar>("bar"))
|
||||||
|
assertEquals("foofoo", context.getBean<FooFoo>().name)
|
||||||
try {
|
try {
|
||||||
context.getBean<Baz>()
|
context.getBean<Baz>()
|
||||||
fail("Expect NoSuchBeanDefinitionException to be thrown")
|
fail("Expect NoSuchBeanDefinitionException to be thrown")
|
||||||
}
|
}
|
||||||
catch(ex: NoSuchBeanDefinitionException) { null }
|
catch(ex: NoSuchBeanDefinitionException) { null }
|
||||||
val foofoo = context.getBean<FooFoo>()
|
|
||||||
assertEquals("foofoo", foofoo.name)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test // SPR-16412
|
@Test // SPR-16412
|
||||||
|
|||||||
Reference in New Issue
Block a user