提交 af6a5566 编写于 作者: S Sebastien Deleuze

Replace context by provider<T>() 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<Foo>() function in order
to provide a more clean API instead.

Issue: SPR-17352
上级 635d2146
......@@ -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<BeanDefinitionDsl>()
/**
* 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 <reified T : Any> provider() : ObjectProvider<T> = context.getBeanProvider()
/**
* Take in account bean definitions enclosed in the provided lambda only when the
* specified profile is active.
......
......@@ -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<Foo>()
bean<Foo>()
bean { BarBar(context.getBeansOfType<Foo>().values) }
bean { BarBar(provider<Foo>().stream().collect(Collectors.toList())) }
}
val context = GenericApplicationContext().apply {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册