From eed0a3ff59751682bc0effae661ca5e500ed75da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Deleuze?= Date: Mon, 26 May 2025 11:55:34 +0200 Subject: [PATCH] Allow access to env from SupplierContextDsl Closes gh-34943 --- .../beans/factory/BeanRegistrarDsl.kt | 6 +++--- .../BeanRegistrarDslConfigurationTests.kt | 20 +++++++++---------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/spring-beans/src/main/kotlin/org/springframework/beans/factory/BeanRegistrarDsl.kt b/spring-beans/src/main/kotlin/org/springframework/beans/factory/BeanRegistrarDsl.kt index 99d2b9b59f4..cb589094595 100644 --- a/spring-beans/src/main/kotlin/org/springframework/beans/factory/BeanRegistrarDsl.kt +++ b/spring-beans/src/main/kotlin/org/springframework/beans/factory/BeanRegistrarDsl.kt @@ -302,7 +302,7 @@ open class BeanRegistrarDsl(private val init: BeanRegistrarDsl.() -> Unit): Bean it.prototype() } it.supplier { - SupplierContextDsl(it).supplier() + SupplierContextDsl(it, env).supplier() } val resolvableType = ResolvableType.forType(object: ParameterizedTypeReference() {}); if (resolvableType.hasGenerics()) { @@ -370,7 +370,7 @@ open class BeanRegistrarDsl(private val init: BeanRegistrarDsl.() -> Unit): Bean it.prototype() } it.supplier { - SupplierContextDsl(it).supplier() + SupplierContextDsl(it, env).supplier() } val resolvableType = ResolvableType.forType(object: ParameterizedTypeReference() {}); if (resolvableType.hasGenerics()) { @@ -1074,7 +1074,7 @@ open class BeanRegistrarDsl(private val init: BeanRegistrarDsl.() -> Unit): Bean * to bean dependencies. */ @BeanRegistrarDslMarker - open class SupplierContextDsl(@PublishedApi internal val context: SupplierContext) { + open class SupplierContextDsl(@PublishedApi internal val context: SupplierContext, val env: Environment) { /** * Return the bean instance that uniquely matches the given object type, diff --git a/spring-context/src/test/kotlin/org/springframework/context/annotation/BeanRegistrarDslConfigurationTests.kt b/spring-context/src/test/kotlin/org/springframework/context/annotation/BeanRegistrarDslConfigurationTests.kt index c8f59673feb..41adc23a5d8 100644 --- a/spring-context/src/test/kotlin/org/springframework/context/annotation/BeanRegistrarDslConfigurationTests.kt +++ b/spring-context/src/test/kotlin/org/springframework/context/annotation/BeanRegistrarDslConfigurationTests.kt @@ -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().foo).isEqualTo(context.getBean()) assertThat(context.getBean("foo")).isEqualTo(context.getBean("fooAlias")) - assertThatThrownBy(ThrowableAssert.ThrowingCallable { context.getBean() }).isInstanceOf(NoSuchBeanDefinitionException::class.java) + assertThatThrownBy { context.getBean() }.isInstanceOf(NoSuchBeanDefinitionException::class.java) assertThat(context.getBean().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().foo).isEqualTo(context.getBean()) assertThat(context.getBean().message).isEqualTo("Hello World!") @@ -101,7 +105,7 @@ class BeanRegistrarDslConfigurationTests { Bar(bean()) } profile("baz") { - registerBean { Baz("Hello World!") } + registerBean { Baz(env.getRequiredProperty("hello.world")) } } registerBean() registerBean(::booFactory, "fooFactory") @@ -113,11 +117,7 @@ class BeanRegistrarDslConfigurationTests { private class GenericBeanRegistrar : BeanRegistrarDsl({ registerBean>(name = "fooSupplier") { - object: Supplier { - override fun get(): Foo { - return Foo() - } - } + Supplier { Foo() } } })