Skip to content

Commit

Permalink
Change BeanDefinitionDsl to implement ApplicationContextInitializer
Browse files Browse the repository at this point in the history
  • Loading branch information
sdeleuze committed Sep 4, 2017
1 parent 2969af8 commit 8b8a676
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.springframework.context.support

import org.springframework.beans.factory.config.BeanDefinitionCustomizer
import org.springframework.context.ApplicationContextInitializer
import org.springframework.core.env.ConfigurableEnvironment
import java.util.function.Supplier

Expand Down Expand Up @@ -75,7 +76,7 @@ fun beans(init: BeanDefinitionDsl.() -> Unit): BeanDefinitionDsl {
* @author Sebastien Deleuze
* @since 5.0
*/
class BeanDefinitionDsl(private val condition: (ConfigurableEnvironment) -> Boolean = { true }) : (GenericApplicationContext) -> Unit {
class BeanDefinitionDsl(private val condition: (ConfigurableEnvironment) -> Boolean = { true }) : ApplicationContextInitializer<GenericApplicationContext> {

@PublishedApi
internal val registrations = arrayListOf<(GenericApplicationContext) -> Unit>()
Expand Down Expand Up @@ -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
*/
override fun invoke(context: GenericApplicationContext) {
override fun initialize(context: GenericApplicationContext) {
for (registration in registrations) {
if (condition.invoke(context.environment)) {
registration.invoke(context)
}
}
for (child in children) {
child.invoke(context)
child.initialize(context)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ class BeanDefinitionDslTests {
bean { Baz(ref("bar")) }
}

val context = GenericApplicationContext()
beans.invoke(context)
context.refresh()
val context = GenericApplicationContext().apply {
beans.initialize(this)
refresh()
}

assertNotNull(context.getBean<Foo>())
assertNotNull(context.getBean<Bar>("bar"))
Expand All @@ -60,9 +61,10 @@ class BeanDefinitionDslTests {
}
}

val context = GenericApplicationContext()
beans.invoke(context)
context.refresh()
val context = GenericApplicationContext().apply {
beans.initialize(this)
refresh()
}

assertNotNull(context.getBean<Foo>())
assertNotNull(context.getBean<Bar>("bar"))
Expand All @@ -87,9 +89,9 @@ class BeanDefinitionDslTests {

val context = GenericApplicationContext().apply {
environment.propertySources.addFirst(SimpleCommandLinePropertySource("--name=foofoo"))
beans.initialize(this)
refresh()
}
beans.invoke(context)
context.refresh()

assertNotNull(context.getBean<Foo>())
assertNotNull(context.getBean<Bar>("bar"))
Expand Down

0 comments on commit 8b8a676

Please sign in to comment.