Skip to content

Commit af6a556

Browse files
committed
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
1 parent 635d214 commit af6a556

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

spring-context/src/main/kotlin/org/springframework/context/support/BeanDefinitionDsl.kt

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616

1717
package org.springframework.context.support
1818

19+
import org.springframework.beans.factory.ObjectProvider
1920
import org.springframework.beans.factory.config.BeanDefinition
2021
import org.springframework.beans.factory.config.BeanDefinitionCustomizer
22+
import org.springframework.beans.factory.getBeanProvider
2123
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils
2224
import org.springframework.context.ApplicationContextInitializer
2325
import org.springframework.core.env.ConfigurableEnvironment
@@ -81,10 +83,10 @@ open class BeanDefinitionDsl(private val init: BeanDefinitionDsl.() -> Unit,
8183
internal val children = arrayListOf<BeanDefinitionDsl>()
8284

8385
/**
84-
* Access to the context for advanced use-cases.
85-
* @since 5.1
86+
* @see provider
8687
*/
87-
lateinit var context: GenericApplicationContext
88+
@PublishedApi
89+
internal lateinit var context: GenericApplicationContext
8890

8991
/**
9092
* Shortcut for `context.environment`
@@ -245,6 +247,15 @@ open class BeanDefinitionDsl(private val init: BeanDefinitionDsl.() -> Unit,
245247
else -> context.getBean(name, T::class.java)
246248
}
247249

250+
251+
/**
252+
* Return an provider for the specified bean, allowing for lazy on-demand retrieval
253+
* of instances, including availability and uniqueness options.
254+
* @since 5.1.1
255+
* @see org.springframework.beans.factory.BeanFactory.getBeanProvider
256+
*/
257+
inline fun <reified T : Any> provider() : ObjectProvider<T> = context.getBeanProvider()
258+
248259
/**
249260
* Take in account bean definitions enclosed in the provided lambda only when the
250261
* specified profile is active.

spring-context/src/test/kotlin/org/springframework/context/support/BeanDefinitionDslTests.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ import org.junit.Assert.*
2020
import org.junit.Test
2121
import org.springframework.beans.factory.NoSuchBeanDefinitionException
2222
import org.springframework.beans.factory.getBean
23-
import org.springframework.beans.factory.getBeansOfType
2423
import org.springframework.context.support.BeanDefinitionDsl.*
2524
import org.springframework.core.env.SimpleCommandLinePropertySource
2625
import org.springframework.core.env.get
2726
import org.springframework.mock.env.MockPropertySource
27+
import java.util.stream.Collectors
2828

2929
@Suppress("UNUSED_EXPRESSION")
3030
class BeanDefinitionDslTests {
@@ -127,12 +127,12 @@ class BeanDefinitionDslTests {
127127
}
128128
}
129129

130-
@Test // SPR-16269
131-
fun `Provide access to the context for allowing calling advanced features like getBeansOfType`() {
130+
@Test // SPR-17352
131+
fun `Retrieve multiple beans via a bean provider`() {
132132
val beans = beans {
133133
bean<Foo>()
134134
bean<Foo>()
135-
bean { BarBar(context.getBeansOfType<Foo>().values) }
135+
bean { BarBar(provider<Foo>().stream().collect(Collectors.toList())) }
136136
}
137137

138138
val context = GenericApplicationContext().apply {

0 commit comments

Comments
 (0)