Change BeanDefinitionDsl to implement ApplicationContextInitializer
This commit is contained in:
parent
2969af82d2
commit
8b8a6766de
|
|
@ -17,6 +17,7 @@
|
||||||
package org.springframework.context.support
|
package org.springframework.context.support
|
||||||
|
|
||||||
import org.springframework.beans.factory.config.BeanDefinitionCustomizer
|
import org.springframework.beans.factory.config.BeanDefinitionCustomizer
|
||||||
|
import org.springframework.context.ApplicationContextInitializer
|
||||||
import org.springframework.core.env.ConfigurableEnvironment
|
import org.springframework.core.env.ConfigurableEnvironment
|
||||||
import java.util.function.Supplier
|
import java.util.function.Supplier
|
||||||
|
|
||||||
|
|
@ -75,7 +76,7 @@ fun beans(init: BeanDefinitionDsl.() -> Unit): BeanDefinitionDsl {
|
||||||
* @author Sebastien Deleuze
|
* @author Sebastien Deleuze
|
||||||
* @since 5.0
|
* @since 5.0
|
||||||
*/
|
*/
|
||||||
class BeanDefinitionDsl(private val condition: (ConfigurableEnvironment) -> Boolean = { true }) : (GenericApplicationContext) -> Unit {
|
class BeanDefinitionDsl(private val condition: (ConfigurableEnvironment) -> Boolean = { true }) : ApplicationContextInitializer<GenericApplicationContext> {
|
||||||
|
|
||||||
@PublishedApi
|
@PublishedApi
|
||||||
internal val registrations = arrayListOf<(GenericApplicationContext) -> Unit>()
|
internal val registrations = arrayListOf<(GenericApplicationContext) -> Unit>()
|
||||||
|
|
@ -210,17 +211,17 @@ class BeanDefinitionDsl(private val condition: (ConfigurableEnvironment) -> Bool
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register the bean defined via the DSL on thAh pe provided application context.
|
* Register the bean defined via the DSL on the provided application context.
|
||||||
* @param context The `ApplicationContext` to use for registering the beans
|
* @param context The `ApplicationContext` to use for registering the beans
|
||||||
*/
|
*/
|
||||||
override fun invoke(context: GenericApplicationContext) {
|
override fun initialize(context: GenericApplicationContext) {
|
||||||
for (registration in registrations) {
|
for (registration in registrations) {
|
||||||
if (condition.invoke(context.environment)) {
|
if (condition.invoke(context.environment)) {
|
||||||
registration.invoke(context)
|
registration.invoke(context)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (child in children) {
|
for (child in children) {
|
||||||
child.invoke(context)
|
child.initialize(context)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -36,9 +36,10 @@ class BeanDefinitionDslTests {
|
||||||
bean { Baz(ref("bar")) }
|
bean { Baz(ref("bar")) }
|
||||||
}
|
}
|
||||||
|
|
||||||
val context = GenericApplicationContext()
|
val context = GenericApplicationContext().apply {
|
||||||
beans.invoke(context)
|
beans.initialize(this)
|
||||||
context.refresh()
|
refresh()
|
||||||
|
}
|
||||||
|
|
||||||
assertNotNull(context.getBean<Foo>())
|
assertNotNull(context.getBean<Foo>())
|
||||||
assertNotNull(context.getBean<Bar>("bar"))
|
assertNotNull(context.getBean<Bar>("bar"))
|
||||||
|
|
@ -60,9 +61,10 @@ class BeanDefinitionDslTests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val context = GenericApplicationContext()
|
val context = GenericApplicationContext().apply {
|
||||||
beans.invoke(context)
|
beans.initialize(this)
|
||||||
context.refresh()
|
refresh()
|
||||||
|
}
|
||||||
|
|
||||||
assertNotNull(context.getBean<Foo>())
|
assertNotNull(context.getBean<Foo>())
|
||||||
assertNotNull(context.getBean<Bar>("bar"))
|
assertNotNull(context.getBean<Bar>("bar"))
|
||||||
|
|
@ -87,9 +89,9 @@ class BeanDefinitionDslTests {
|
||||||
|
|
||||||
val context = GenericApplicationContext().apply {
|
val context = GenericApplicationContext().apply {
|
||||||
environment.propertySources.addFirst(SimpleCommandLinePropertySource("--name=foofoo"))
|
environment.propertySources.addFirst(SimpleCommandLinePropertySource("--name=foofoo"))
|
||||||
|
beans.initialize(this)
|
||||||
|
refresh()
|
||||||
}
|
}
|
||||||
beans.invoke(context)
|
|
||||||
context.refresh()
|
|
||||||
|
|
||||||
assertNotNull(context.getBean<Foo>())
|
assertNotNull(context.getBean<Foo>())
|
||||||
assertNotNull(context.getBean<Bar>("bar"))
|
assertNotNull(context.getBean<Bar>("bar"))
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue