From af6a5566a30adcace0a05ed7285e7f40e49d8f0b Mon Sep 17 00:00:00 2001 From: Sebastien Deleuze Date: Mon, 8 Oct 2018 13:08:00 +0200 Subject: [PATCH] Replace context by provider() in Kotlin bean DSL Spring Framework 5.1.0 exposed by mistake context in the Kotlin bean DSL API in order to fix SPR-16269. Now that BeanFactory#getBeanprovider is available, it should be exposed via a provider() function in order to provide a more clean API instead. Issue: SPR-17352 --- .../context/support/BeanDefinitionDsl.kt | 17 ++++++++++++++--- .../context/support/BeanDefinitionDslTests.kt | 8 ++++---- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/spring-context/src/main/kotlin/org/springframework/context/support/BeanDefinitionDsl.kt b/spring-context/src/main/kotlin/org/springframework/context/support/BeanDefinitionDsl.kt index fe7826e424..31b0255412 100644 --- a/spring-context/src/main/kotlin/org/springframework/context/support/BeanDefinitionDsl.kt +++ b/spring-context/src/main/kotlin/org/springframework/context/support/BeanDefinitionDsl.kt @@ -16,8 +16,10 @@ package org.springframework.context.support +import org.springframework.beans.factory.ObjectProvider import org.springframework.beans.factory.config.BeanDefinition import org.springframework.beans.factory.config.BeanDefinitionCustomizer +import org.springframework.beans.factory.getBeanProvider import org.springframework.beans.factory.support.BeanDefinitionReaderUtils import org.springframework.context.ApplicationContextInitializer import org.springframework.core.env.ConfigurableEnvironment @@ -81,10 +83,10 @@ open class BeanDefinitionDsl(private val init: BeanDefinitionDsl.() -> Unit, internal val children = arrayListOf() /** - * Access to the context for advanced use-cases. - * @since 5.1 + * @see provider */ - lateinit var context: GenericApplicationContext + @PublishedApi + internal lateinit var context: GenericApplicationContext /** * Shortcut for `context.environment` @@ -245,6 +247,15 @@ open class BeanDefinitionDsl(private val init: BeanDefinitionDsl.() -> Unit, else -> context.getBean(name, T::class.java) } + + /** + * Return an provider for the specified bean, allowing for lazy on-demand retrieval + * of instances, including availability and uniqueness options. + * @since 5.1.1 + * @see org.springframework.beans.factory.BeanFactory.getBeanProvider + */ + inline fun provider() : ObjectProvider = context.getBeanProvider() + /** * Take in account bean definitions enclosed in the provided lambda only when the * specified profile is active. diff --git a/spring-context/src/test/kotlin/org/springframework/context/support/BeanDefinitionDslTests.kt b/spring-context/src/test/kotlin/org/springframework/context/support/BeanDefinitionDslTests.kt index 794e55576a..d69e069cd5 100644 --- a/spring-context/src/test/kotlin/org/springframework/context/support/BeanDefinitionDslTests.kt +++ b/spring-context/src/test/kotlin/org/springframework/context/support/BeanDefinitionDslTests.kt @@ -20,11 +20,11 @@ import org.junit.Assert.* import org.junit.Test import org.springframework.beans.factory.NoSuchBeanDefinitionException import org.springframework.beans.factory.getBean -import org.springframework.beans.factory.getBeansOfType import org.springframework.context.support.BeanDefinitionDsl.* import org.springframework.core.env.SimpleCommandLinePropertySource import org.springframework.core.env.get import org.springframework.mock.env.MockPropertySource +import java.util.stream.Collectors @Suppress("UNUSED_EXPRESSION") class BeanDefinitionDslTests { @@ -127,12 +127,12 @@ class BeanDefinitionDslTests { } } - @Test // SPR-16269 - fun `Provide access to the context for allowing calling advanced features like getBeansOfType`() { + @Test // SPR-17352 + fun `Retrieve multiple beans via a bean provider`() { val beans = beans { bean() bean() - bean { BarBar(context.getBeansOfType().values) } + bean { BarBar(provider().stream().collect(Collectors.toList())) } } val context = GenericApplicationContext().apply {