Skip to content

Commit 0ad9349

Browse files
committed
Limit ref() and provider() visibility in Kotlin DSL
This commit makes ref() and provider() only available from inside the bean lambda and not from the root level of the beans DSL. Closes gh-22177
1 parent 34ddb1e commit 0ad9349

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

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

+26-20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -83,7 +83,7 @@ open class BeanDefinitionDsl(private val init: BeanDefinitionDsl.() -> Unit,
8383
internal val children = arrayListOf<BeanDefinitionDsl>()
8484

8585
/**
86-
* @see provider
86+
* @see BeanSupplierContext
8787
*/
8888
@PublishedApi
8989
internal lateinit var context: GenericApplicationContext
@@ -217,7 +217,7 @@ open class BeanDefinitionDsl(private val init: BeanDefinitionDsl.() -> Unit,
217217
destroyMethodName: String? = null,
218218
description: String? = null,
219219
role: Role? = null,
220-
crossinline function: () -> T) {
220+
crossinline function: BeanSupplierContext.() -> T) {
221221

222222
val customizer = BeanDefinitionCustomizer { bd ->
223223
scope?.let { bd.scope = scope.name.toLowerCase() }
@@ -232,29 +232,35 @@ open class BeanDefinitionDsl(private val init: BeanDefinitionDsl.() -> Unit,
232232

233233

234234
val beanName = name ?: BeanDefinitionReaderUtils.uniqueBeanName(T::class.java.name, context);
235-
context.registerBean(beanName, T::class.java, Supplier { function.invoke() }, customizer)
235+
context.registerBean(beanName, T::class.java, Supplier { function.invoke(BeanSupplierContext(context)) }, customizer)
236236
}
237237

238238
/**
239-
* Get a reference to the bean by type or type + name with the syntax
240-
* `ref<Foo>()` or `ref<Foo>("foo")`. When leveraging Kotlin type inference
241-
* it could be as short as `ref()` or `ref("foo")`.
242-
* @param name the name of the bean to retrieve
243-
* @param T type the bean must match, can be an interface or superclass
239+
* Limit access to `ref()` and `provider()` to bean supplier lambdas.
240+
* @since 5.2
244241
*/
245-
inline fun <reified T : Any> ref(name: String? = null) : T = when (name) {
246-
null -> context.getBean(T::class.java)
247-
else -> context.getBean(name, T::class.java)
248-
}
242+
open class BeanSupplierContext(@PublishedApi internal val context: GenericApplicationContext) {
249243

244+
/**
245+
* Get a reference to the bean by type or type + name with the syntax
246+
* `ref<Foo>()` or `ref<Foo>("foo")`. When leveraging Kotlin type inference
247+
* it could be as short as `ref()` or `ref("foo")`.
248+
* @param name the name of the bean to retrieve
249+
* @param T type the bean must match, can be an interface or superclass
250+
*/
251+
inline fun <reified T : Any> ref(name: String? = null) : T = when (name) {
252+
null -> context.getBean(T::class.java)
253+
else -> context.getBean(name, T::class.java)
254+
}
250255

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()
256+
/**
257+
* Return an provider for the specified bean, allowing for lazy on-demand retrieval
258+
* of instances, including availability and uniqueness options.
259+
* @see org.springframework.beans.factory.BeanFactory.getBeanProvider
260+
*/
261+
inline fun <reified T : Any> provider() : ObjectProvider<T> = context.getBeanProvider()
262+
263+
}
258264

259265
/**
260266
* Take in account bean definitions enclosed in the provided lambda only when the

0 commit comments

Comments
 (0)