1
1
/*
2
- * Copyright 2002-2018 the original author or authors.
2
+ * Copyright 2002-2019 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -83,7 +83,7 @@ open class BeanDefinitionDsl(private val init: BeanDefinitionDsl.() -> Unit,
83
83
internal val children = arrayListOf<BeanDefinitionDsl >()
84
84
85
85
/* *
86
- * @see provider
86
+ * @see BeanSupplierContext
87
87
*/
88
88
@PublishedApi
89
89
internal lateinit var context: GenericApplicationContext
@@ -217,7 +217,7 @@ open class BeanDefinitionDsl(private val init: BeanDefinitionDsl.() -> Unit,
217
217
destroyMethodName : String? = null,
218
218
description : String? = null,
219
219
role : Role ? = null,
220
- crossinline function : () -> T ) {
220
+ crossinline function : BeanSupplierContext . () -> T ) {
221
221
222
222
val customizer = BeanDefinitionCustomizer { bd ->
223
223
scope?.let { bd.scope = scope.name.toLowerCase() }
@@ -232,29 +232,35 @@ open class BeanDefinitionDsl(private val init: BeanDefinitionDsl.() -> Unit,
232
232
233
233
234
234
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)
236
236
}
237
237
238
238
/* *
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
244
241
*/
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 ) {
249
243
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
+ }
250
255
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
+ }
258
264
259
265
/* *
260
266
* Take in account bean definitions enclosed in the provided lambda only when the
0 commit comments