Skip to content

Commit 3f1882c

Browse files
committed
Support noarg callable references in Kotlin beans DSL
Closes gh-23395
1 parent 0801a7d commit 3f1882c

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,42 @@ open class BeanDefinitionDsl internal constructor (private val init: BeanDefinit
236236
context.registerBean(beanName, T::class.java, Supplier { function.invoke(BeanSupplierContext(context)) }, customizer)
237237
}
238238

239+
/**
240+
* Declare a bean definition using the given callable reference with no parameter
241+
* for obtaining a new instance.
242+
*
243+
* @param f the callable reference
244+
* @param name the name of the bean
245+
* @param scope Override the target scope of this bean, specifying a new scope name.
246+
* @param isLazyInit Set whether this bean should be lazily initialized.
247+
* @param isPrimary Set whether this bean is a primary autowire candidate.
248+
* @param isAutowireCandidate Set whether this bean is a candidate for getting
249+
* autowired into some other bean.
250+
* @param initMethodName Set the name of the initializer method
251+
* @param destroyMethodName Set the name of the destroy method
252+
* @param description Set a human-readable description of this bean definition
253+
* @param role Set the role hint for this bean definition
254+
* @see GenericApplicationContext.registerBean
255+
* @see org.springframework.beans.factory.config.BeanDefinition
256+
* @since 5.2.3
257+
*/
258+
inline fun <reified T: Any>
259+
bean(crossinline f: () -> T,
260+
name: String? = null,
261+
scope: BeanDefinitionDsl.Scope? = null,
262+
isLazyInit: Boolean? = null,
263+
isPrimary: Boolean? = null,
264+
isAutowireCandidate: Boolean? = null,
265+
initMethodName: String? = null,
266+
destroyMethodName: String? = null,
267+
description: String? = null,
268+
role: BeanDefinitionDsl.Role? = null) {
269+
270+
bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) {
271+
f.invoke()
272+
}
273+
}
274+
239275
/**
240276
* Declare a bean definition using the given callable reference with 1 parameter
241277
* autowired by type for obtaining a new instance.

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -156,6 +156,7 @@ class BeanDefinitionDslTests {
156156
val beans = beans {
157157
bean<Bar>()
158158
bean(::baz)
159+
bean(::foo)
159160
}
160161
val context = GenericApplicationContext().apply {
161162
beans.initialize(this)
@@ -205,3 +206,4 @@ class FooFoo(val name: String)
205206
class BarBar(val foos: Collection<Foo>)
206207

207208
fun baz(bar: Bar) = Baz(bar)
209+
fun foo() = Foo()

0 commit comments

Comments
 (0)