Add an env accessor to BeanDefinitionContext

Issue: SPR-15755
This commit is contained in:
Sebastien Deleuze 2017-07-18 16:19:34 +02:00
parent c292a89b24
commit f72e0daa54
2 changed files with 17 additions and 3 deletions

View File

@ -17,7 +17,6 @@
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.ApplicationContext
import org.springframework.core.env.ConfigurableEnvironment import org.springframework.core.env.ConfigurableEnvironment
import java.util.function.Supplier import java.util.function.Supplier
@ -38,12 +37,20 @@ open class BeanDefinitionDsl(val condition: (ConfigurableEnvironment) -> Boolean
PROTOTYPE PROTOTYPE
} }
class BeanDefinitionContext(val context: ApplicationContext) { class BeanDefinitionContext(val context: GenericApplicationContext) {
inline fun <reified T : Any> ref(name: String? = null) : T = when (name) { inline fun <reified T : Any> ref(name: String? = null) : T = when (name) {
null -> context.getBean(T::class.java) null -> context.getBean(T::class.java)
else -> context.getBean(name, T::class.java) else -> context.getBean(name, T::class.java)
} }
/**
* Get the [ConfigurableEnvironment] associated to the underlying [GenericApplicationContext].
*/
val env : ConfigurableEnvironment
get() = context.environment
} }
/** /**

View File

@ -21,6 +21,7 @@ import org.junit.Test
import org.springframework.beans.factory.NoSuchBeanDefinitionException import org.springframework.beans.factory.NoSuchBeanDefinitionException
import org.springframework.beans.factory.getBean import org.springframework.beans.factory.getBean
import org.springframework.context.support.BeanDefinitionDsl.* import org.springframework.context.support.BeanDefinitionDsl.*
import org.springframework.core.env.SimpleCommandLinePropertySource
class BeanDefinitionDslTests { class BeanDefinitionDslTests {
@ -75,13 +76,16 @@ class BeanDefinitionDslTests {
val beans = beans { val beans = beans {
bean<Foo>() bean<Foo>()
bean<Bar>("bar") bean<Bar>("bar")
bean { FooFoo(it.env.getProperty("name")) }
environment({it.activeProfiles.contains("baz")}) { environment({it.activeProfiles.contains("baz")}) {
bean { Baz(it.ref()) } bean { Baz(it.ref()) }
bean { Baz(it.ref("bar")) } bean { Baz(it.ref("bar")) }
} }
} }
val context = GenericApplicationContext() val context = GenericApplicationContext().apply {
environment.propertySources.addFirst(SimpleCommandLinePropertySource("--name=foofoo"))
}
beans.invoke(context) beans.invoke(context)
context.refresh() context.refresh()
@ -92,6 +96,8 @@ class BeanDefinitionDslTests {
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)
} }
} }
@ -99,3 +105,4 @@ class BeanDefinitionDslTests {
class Foo class Foo
class Bar class Bar
class Baz(val bar: Bar) class Baz(val bar: Bar)
class FooFoo(val name: String)