Allow access to env from SupplierContextDsl
Closes gh-34943
This commit is contained in:
parent
a8ac54120d
commit
eed0a3ff59
|
@ -302,7 +302,7 @@ open class BeanRegistrarDsl(private val init: BeanRegistrarDsl.() -> Unit): Bean
|
|||
it.prototype()
|
||||
}
|
||||
it.supplier {
|
||||
SupplierContextDsl<T>(it).supplier()
|
||||
SupplierContextDsl<T>(it, env).supplier()
|
||||
}
|
||||
val resolvableType = ResolvableType.forType(object: ParameterizedTypeReference<T>() {});
|
||||
if (resolvableType.hasGenerics()) {
|
||||
|
@ -370,7 +370,7 @@ open class BeanRegistrarDsl(private val init: BeanRegistrarDsl.() -> Unit): Bean
|
|||
it.prototype()
|
||||
}
|
||||
it.supplier {
|
||||
SupplierContextDsl<T>(it).supplier()
|
||||
SupplierContextDsl<T>(it, env).supplier()
|
||||
}
|
||||
val resolvableType = ResolvableType.forType(object: ParameterizedTypeReference<T>() {});
|
||||
if (resolvableType.hasGenerics()) {
|
||||
|
@ -1074,7 +1074,7 @@ open class BeanRegistrarDsl(private val init: BeanRegistrarDsl.() -> Unit): Bean
|
|||
* to bean dependencies.
|
||||
*/
|
||||
@BeanRegistrarDslMarker
|
||||
open class SupplierContextDsl<T>(@PublishedApi internal val context: SupplierContext) {
|
||||
open class SupplierContextDsl<T>(@PublishedApi internal val context: SupplierContext, val env: Environment) {
|
||||
|
||||
/**
|
||||
* Return the bean instance that uniquely matches the given object type,
|
||||
|
|
|
@ -18,7 +18,6 @@ package org.springframework.context.annotation
|
|||
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.assertj.core.api.Assertions.assertThatThrownBy
|
||||
import org.assertj.core.api.ThrowableAssert
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.springframework.beans.factory.BeanRegistrarDsl
|
||||
import org.springframework.beans.factory.InitializingBean
|
||||
|
@ -26,6 +25,7 @@ import org.springframework.beans.factory.NoSuchBeanDefinitionException
|
|||
import org.springframework.beans.factory.config.BeanDefinition
|
||||
import org.springframework.beans.factory.getBean
|
||||
import org.springframework.beans.factory.support.RootBeanDefinition
|
||||
import org.springframework.mock.env.MockEnvironment
|
||||
import java.util.function.Supplier
|
||||
|
||||
/**
|
||||
|
@ -37,10 +37,13 @@ class BeanRegistrarDslConfigurationTests {
|
|||
|
||||
@Test
|
||||
fun beanRegistrar() {
|
||||
val context = AnnotationConfigApplicationContext(BeanRegistrarKotlinConfiguration::class.java)
|
||||
val context = AnnotationConfigApplicationContext()
|
||||
context.register(BeanRegistrarKotlinConfiguration::class.java)
|
||||
context.environment = MockEnvironment().withProperty("hello.world", "Hello World!")
|
||||
context.refresh()
|
||||
assertThat(context.getBean<Bar>().foo).isEqualTo(context.getBean<Foo>())
|
||||
assertThat(context.getBean<Foo>("foo")).isEqualTo(context.getBean<Foo>("fooAlias"))
|
||||
assertThatThrownBy(ThrowableAssert.ThrowingCallable { context.getBean<Baz>() }).isInstanceOf(NoSuchBeanDefinitionException::class.java)
|
||||
assertThatThrownBy { context.getBean<Baz>() }.isInstanceOf(NoSuchBeanDefinitionException::class.java)
|
||||
assertThat(context.getBean<Init>().initialized).isTrue()
|
||||
val beanDefinition = context.getBeanDefinition("bar")
|
||||
assertThat(beanDefinition.scope).isEqualTo(BeanDefinition.SCOPE_PROTOTYPE)
|
||||
|
@ -53,7 +56,8 @@ class BeanRegistrarDslConfigurationTests {
|
|||
fun beanRegistrarWithProfile() {
|
||||
val context = AnnotationConfigApplicationContext()
|
||||
context.register(BeanRegistrarKotlinConfiguration::class.java)
|
||||
context.getEnvironment().addActiveProfile("baz")
|
||||
context.environment = MockEnvironment().withProperty("hello.world", "Hello World!")
|
||||
context.environment.addActiveProfile("baz")
|
||||
context.refresh()
|
||||
assertThat(context.getBean<Bar>().foo).isEqualTo(context.getBean<Foo>())
|
||||
assertThat(context.getBean<Baz>().message).isEqualTo("Hello World!")
|
||||
|
@ -101,7 +105,7 @@ class BeanRegistrarDslConfigurationTests {
|
|||
Bar(bean<Foo>())
|
||||
}
|
||||
profile("baz") {
|
||||
registerBean { Baz("Hello World!") }
|
||||
registerBean { Baz(env.getRequiredProperty("hello.world")) }
|
||||
}
|
||||
registerBean<Init>()
|
||||
registerBean(::booFactory, "fooFactory")
|
||||
|
@ -113,11 +117,7 @@ class BeanRegistrarDslConfigurationTests {
|
|||
|
||||
private class GenericBeanRegistrar : BeanRegistrarDsl({
|
||||
registerBean<Supplier<Foo>>(name = "fooSupplier") {
|
||||
object: Supplier<Foo> {
|
||||
override fun get(): Foo {
|
||||
return Foo()
|
||||
}
|
||||
}
|
||||
Supplier<Foo> { Foo() }
|
||||
}
|
||||
})
|
||||
|
||||
|
|
Loading…
Reference in New Issue