From fb1da014103b821f93859dca20cc50f144e49e22 Mon Sep 17 00:00:00 2001 From: Sebastien Deleuze Date: Wed, 27 Mar 2019 14:30:39 +0100 Subject: [PATCH] Support creating beans from Kotlin callable references This commit updates Kotlin beans DSL in order to support creating beans using callable references with autowired parameters. Type resolution is implemented using Kotlin reified type parameters without requiring reflection. Closes gh-21845 --- .../context/support/BeanDefinitionDsl.kt | 819 ++++++++++++++++++ .../context/support/BeanDefinitionDslTests.kt | 15 + src/docs/asciidoc/languages/kotlin.adoc | 38 +- 3 files changed, 867 insertions(+), 5 deletions(-) diff --git a/spring-context/src/main/kotlin/org/springframework/context/support/BeanDefinitionDsl.kt b/spring-context/src/main/kotlin/org/springframework/context/support/BeanDefinitionDsl.kt index f3fd050aa30..ca155cc5e04 100644 --- a/spring-context/src/main/kotlin/org/springframework/context/support/BeanDefinitionDsl.kt +++ b/spring-context/src/main/kotlin/org/springframework/context/support/BeanDefinitionDsl.kt @@ -235,6 +235,825 @@ open class BeanDefinitionDsl(private val init: BeanDefinitionDsl.() -> Unit, context.registerBean(beanName, T::class.java, Supplier { function.invoke(BeanSupplierContext(context)) }, customizer) } + /** + * Declare a bean definition using the given callable reference with 1 parameter + * autowired by type for obtaining a new instance. + * + * @param f the callable reference + * @param name the name of the bean + * @param scope Override the target scope of this bean, specifying a new scope name. + * @param isLazyInit Set whether this bean should be lazily initialized. + * @param isPrimary Set whether this bean is a primary autowire candidate. + * @param isAutowireCandidate Set whether this bean is a candidate for getting + * autowired into some other bean. + * @param initMethodName Set the name of the initializer method + * @param destroyMethodName Set the name of the destroy method + * @param description Set a human-readable description of this bean definition + * @param role Set the role hint for this bean definition + * @see GenericApplicationContext.registerBean + * @see org.springframework.beans.factory.config.BeanDefinition + * @since 5.2 + */ + inline fun + bean(crossinline f: (A) -> T, + name: String? = null, + scope: BeanDefinitionDsl.Scope? = null, + isLazyInit: Boolean? = null, + isPrimary: Boolean? = null, + isAutowireCandidate: Boolean? = null, + initMethodName: String? = null, + destroyMethodName: String? = null, + description: String? = null, + role: BeanDefinitionDsl.Role? = null) { + + bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) { + f.invoke(ref()) + } + } + + /** + * Declare a bean definition using the given callable reference with 2 parameters + * autowired by type for obtaining a new instance. + * + * @param f the callable reference + * @param name the name of the bean + * @param scope Override the target scope of this bean, specifying a new scope name. + * @param isLazyInit Set whether this bean should be lazily initialized. + * @param isPrimary Set whether this bean is a primary autowire candidate. + * @param isAutowireCandidate Set whether this bean is a candidate for getting + * autowired into some other bean. + * @param initMethodName Set the name of the initializer method + * @param destroyMethodName Set the name of the destroy method + * @param description Set a human-readable description of this bean definition + * @param role Set the role hint for this bean definition + * @see GenericApplicationContext.registerBean + * @see org.springframework.beans.factory.config.BeanDefinition + * @since 5.2 + */ + inline fun + bean(crossinline f: (A, B) -> T, + name: String? = null, + scope: BeanDefinitionDsl.Scope? = null, + isLazyInit: Boolean? = null, + isPrimary: Boolean? = null, + isAutowireCandidate: Boolean? = null, + initMethodName: String? = null, + destroyMethodName: String? = null, + description: String? = null, + role: BeanDefinitionDsl.Role? = null) { + + bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) { + f.invoke(ref(), ref()) + } + } + + /** + * Declare a bean definition using the given callable reference with 3 parameters + * autowired by type for obtaining a new instance. + * + * @param f the callable reference + * @param name the name of the bean + * @param scope Override the target scope of this bean, specifying a new scope name. + * @param isLazyInit Set whether this bean should be lazily initialized. + * @param isPrimary Set whether this bean is a primary autowire candidate. + * @param isAutowireCandidate Set whether this bean is a candidate for getting + * autowired into some other bean. + * @param initMethodName Set the name of the initializer method + * @param destroyMethodName Set the name of the destroy method + * @param description Set a human-readable description of this bean definition + * @param role Set the role hint for this bean definition + * @see GenericApplicationContext.registerBean + * @see org.springframework.beans.factory.config.BeanDefinition + * @since 5.2 + */ + inline fun + bean(crossinline f: (A, B, C) -> T, + name: String? = null, + scope: BeanDefinitionDsl.Scope? = null, + isLazyInit: Boolean? = null, + isPrimary: Boolean? = null, + isAutowireCandidate: Boolean? = null, + initMethodName: String? = null, + destroyMethodName: String? = null, + description: String? = null, + role: BeanDefinitionDsl.Role? = null) { + + bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) { + f.invoke(ref(), ref(), ref()) + } + } + + /** + * Declare a bean definition using the given callable reference with 4 parameters + * autowired by type for obtaining a new instance. + * + * @param f the callable reference + * @param name the name of the bean + * @param scope Override the target scope of this bean, specifying a new scope name. + * @param isLazyInit Set whether this bean should be lazily initialized. + * @param isPrimary Set whether this bean is a primary autowire candidate. + * @param isAutowireCandidate Set whether this bean is a candidate for getting + * autowired into some other bean. + * @param initMethodName Set the name of the initializer method + * @param destroyMethodName Set the name of the destroy method + * @param description Set a human-readable description of this bean definition + * @param role Set the role hint for this bean definition + * @see GenericApplicationContext.registerBean + * @see org.springframework.beans.factory.config.BeanDefinition + * @since 5.2 + */ + inline fun + bean(crossinline f: (A, B, C, D) -> T, + name: String? = null, + scope: BeanDefinitionDsl.Scope? = null, + isLazyInit: Boolean? = null, + isPrimary: Boolean? = null, + isAutowireCandidate: Boolean? = null, + initMethodName: String? = null, + destroyMethodName: String? = null, + description: String? = null, + role: BeanDefinitionDsl.Role? = null) { + + bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) { + f.invoke(ref(), ref(), ref(), ref()) + } + } + + /** + * Declare a bean definition using the given callable reference with 5 parameters + * autowired by type for obtaining a new instance. + * + * @param f the callable reference + * @param name the name of the bean + * @param scope Override the target scope of this bean, specifying a new scope name. + * @param isLazyInit Set whether this bean should be lazily initialized. + * @param isPrimary Set whether this bean is a primary autowire candidate. + * @param isAutowireCandidate Set whether this bean is a candidate for getting + * autowired into some other bean. + * @param initMethodName Set the name of the initializer method + * @param destroyMethodName Set the name of the destroy method + * @param description Set a human-readable description of this bean definition + * @param role Set the role hint for this bean definition + * @see GenericApplicationContext.registerBean + * @see org.springframework.beans.factory.config.BeanDefinition + * @since 5.2 + */ + inline fun + bean(crossinline f: (A, B, C, D, E) -> T, + name: String? = null, + scope: BeanDefinitionDsl.Scope? = null, + isLazyInit: Boolean? = null, + isPrimary: Boolean? = null, + isAutowireCandidate: Boolean? = null, + initMethodName: String? = null, + destroyMethodName: String? = null, + description: String? = null, + role: BeanDefinitionDsl.Role? = null) { + + bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) { + f.invoke(ref(), ref(), ref(), ref(), ref()) + } + } + + /** + * Declare a bean definition using the given callable reference with 6 parameters + * autowired by type for obtaining a new instance. + * + * @param f the callable reference + * @param name the name of the bean + * @param scope Override the target scope of this bean, specifying a new scope name. + * @param isLazyInit Set whether this bean should be lazily initialized. + * @param isPrimary Set whether this bean is a primary autowire candidate. + * @param isAutowireCandidate Set whether this bean is a candidate for getting + * autowired into some other bean. + * @param initMethodName Set the name of the initializer method + * @param destroyMethodName Set the name of the destroy method + * @param description Set a human-readable description of this bean definition + * @param role Set the role hint for this bean definition + * @see GenericApplicationContext.registerBean + * @see org.springframework.beans.factory.config.BeanDefinition + * @since 5.2 + */ + inline fun + bean(crossinline f: (A, B, C, D, E, F) -> T, + name: String? = null, + scope: BeanDefinitionDsl.Scope? = null, + isLazyInit: Boolean? = null, + isPrimary: Boolean? = null, + isAutowireCandidate: Boolean? = null, + initMethodName: String? = null, + destroyMethodName: String? = null, + description: String? = null, + role: BeanDefinitionDsl.Role? = null) { + + bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) { + f.invoke(ref(), ref(), ref(), ref(), ref(), ref()) + } + } + + /** + * Declare a bean definition using the given callable reference with 7 parameters + * autowired by type for obtaining a new instance. + * + * @param f the callable reference + * @param name the name of the bean + * @param scope Override the target scope of this bean, specifying a new scope name. + * @param isLazyInit Set whether this bean should be lazily initialized. + * @param isPrimary Set whether this bean is a primary autowire candidate. + * @param isAutowireCandidate Set whether this bean is a candidate for getting + * autowired into some other bean. + * @param initMethodName Set the name of the initializer method + * @param destroyMethodName Set the name of the destroy method + * @param description Set a human-readable description of this bean definition + * @param role Set the role hint for this bean definition + * @see GenericApplicationContext.registerBean + * @see org.springframework.beans.factory.config.BeanDefinition + * @since 5.2 + */ + inline fun + bean(crossinline f: (A, B, C, D, E, F, G) -> T, + name: String? = null, + scope: BeanDefinitionDsl.Scope? = null, + isLazyInit: Boolean? = null, + isPrimary: Boolean? = null, + isAutowireCandidate: Boolean? = null, + initMethodName: String? = null, + destroyMethodName: String? = null, + description: String? = null, + role: BeanDefinitionDsl.Role? = null) { + + bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) { + f.invoke(ref(), ref(), ref(), ref(), ref(), ref(), ref()) + } + } + + /** + * Declare a bean definition using the given callable reference with 8 parameters + * autowired by type for obtaining a new instance. + * + * @param f the callable reference + * @param name the name of the bean + * @param scope Override the target scope of this bean, specifying a new scope name. + * @param isLazyInit Set whether this bean should be lazily initialized. + * @param isPrimary Set whether this bean is a primary autowire candidate. + * @param isAutowireCandidate Set whether this bean is a candidate for getting + * autowired into some other bean. + * @param initMethodName Set the name of the initializer method + * @param destroyMethodName Set the name of the destroy method + * @param description Set a human-readable description of this bean definition + * @param role Set the role hint for this bean definition + * @see GenericApplicationContext.registerBean + * @see org.springframework.beans.factory.config.BeanDefinition + * @since 5.2 + */ + inline fun + bean(crossinline f: (A, B, C, D, E, F, G, H) -> T, + name: String? = null, + scope: BeanDefinitionDsl.Scope? = null, + isLazyInit: Boolean? = null, + isPrimary: Boolean? = null, + isAutowireCandidate: Boolean? = null, + initMethodName: String? = null, + destroyMethodName: String? = null, + description: String? = null, + role: BeanDefinitionDsl.Role? = null) { + + bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) { + f.invoke(ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref()) + } + } + + /** + * Declare a bean definition using the given callable reference with 9 parameters + * autowired by type for obtaining a new instance. + * + * @param f the callable reference + * @param name the name of the bean + * @param scope Override the target scope of this bean, specifying a new scope name. + * @param isLazyInit Set whether this bean should be lazily initialized. + * @param isPrimary Set whether this bean is a primary autowire candidate. + * @param isAutowireCandidate Set whether this bean is a candidate for getting + * autowired into some other bean. + * @param initMethodName Set the name of the initializer method + * @param destroyMethodName Set the name of the destroy method + * @param description Set a human-readable description of this bean definition + * @param role Set the role hint for this bean definition + * @see GenericApplicationContext.registerBean + * @see org.springframework.beans.factory.config.BeanDefinition + * @since 5.2 + */ + inline fun + bean(crossinline f: (A, B, C, D, E, F, G, H, I) -> T, + name: String? = null, + scope: BeanDefinitionDsl.Scope? = null, + isLazyInit: Boolean? = null, + isPrimary: Boolean? = null, + isAutowireCandidate: Boolean? = null, + initMethodName: String? = null, + destroyMethodName: String? = null, + description: String? = null, + role: BeanDefinitionDsl.Role? = null) { + + bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) { + f.invoke(ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref()) + } + } + + /** + * Declare a bean definition using the given callable reference with 10 parameters + * autowired by type for obtaining a new instance. + * + * @param f the callable reference + * @param name the name of the bean + * @param scope Override the target scope of this bean, specifying a new scope name. + * @param isLazyInit Set whether this bean should be lazily initialized. + * @param isPrimary Set whether this bean is a primary autowire candidate. + * @param isAutowireCandidate Set whether this bean is a candidate for getting + * autowired into some other bean. + * @param initMethodName Set the name of the initializer method + * @param destroyMethodName Set the name of the destroy method + * @param description Set a human-readable description of this bean definition + * @param role Set the role hint for this bean definition + * @see GenericApplicationContext.registerBean + * @see org.springframework.beans.factory.config.BeanDefinition + * @since 5.2 + */ + inline fun + bean(crossinline f: (A, B, C, D, E, F, G, H, I, J) -> T, + name: String? = null, + scope: BeanDefinitionDsl.Scope? = null, + isLazyInit: Boolean? = null, + isPrimary: Boolean? = null, + isAutowireCandidate: Boolean? = null, + initMethodName: String? = null, + destroyMethodName: String? = null, + description: String? = null, + role: BeanDefinitionDsl.Role? = null) { + + bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) { + f.invoke(ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref()) + } + } + + /** + * Declare a bean definition using the given callable reference with 11 parameters + * autowired by type for obtaining a new instance. + * + * @param f the callable reference + * @param name the name of the bean + * @param scope Override the target scope of this bean, specifying a new scope name. + * @param isLazyInit Set whether this bean should be lazily initialized. + * @param isPrimary Set whether this bean is a primary autowire candidate. + * @param isAutowireCandidate Set whether this bean is a candidate for getting + * autowired into some other bean. + * @param initMethodName Set the name of the initializer method + * @param destroyMethodName Set the name of the destroy method + * @param description Set a human-readable description of this bean definition + * @param role Set the role hint for this bean definition + * @see GenericApplicationContext.registerBean + * @see org.springframework.beans.factory.config.BeanDefinition + * @since 5.2 + */ + inline fun + bean(crossinline f: (A, B, C, D, E, F, G, H, I, J, K) -> T, + name: String? = null, + scope: BeanDefinitionDsl.Scope? = null, + isLazyInit: Boolean? = null, + isPrimary: Boolean? = null, + isAutowireCandidate: Boolean? = null, + initMethodName: String? = null, + destroyMethodName: String? = null, + description: String? = null, + role: BeanDefinitionDsl.Role? = null) { + + bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) { + f.invoke(ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref()) + } + } + + /** + * Declare a bean definition using the given callable reference with 12 parameters + * autowired by type for obtaining a new instance. + * + * @param f the callable reference + * @param name the name of the bean + * @param scope Override the target scope of this bean, specifying a new scope name. + * @param isLazyInit Set whether this bean should be lazily initialized. + * @param isPrimary Set whether this bean is a primary autowire candidate. + * @param isAutowireCandidate Set whether this bean is a candidate for getting + * autowired into some other bean. + * @param initMethodName Set the name of the initializer method + * @param destroyMethodName Set the name of the destroy method + * @param description Set a human-readable description of this bean definition + * @param role Set the role hint for this bean definition + * @see GenericApplicationContext.registerBean + * @see org.springframework.beans.factory.config.BeanDefinition + * @since 5.2 + */ + inline fun + bean(crossinline f: (A, B, C, D, E, F, G, H, I, J, K, L) -> T, + name: String? = null, + scope: BeanDefinitionDsl.Scope? = null, + isLazyInit: Boolean? = null, + isPrimary: Boolean? = null, + isAutowireCandidate: Boolean? = null, + initMethodName: String? = null, + destroyMethodName: String? = null, + description: String? = null, + role: BeanDefinitionDsl.Role? = null) { + + bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) { + f.invoke(ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref()) + } + } + + /** + * Declare a bean definition using the given callable reference with 13 parameters + * autowired by type for obtaining a new instance. + * + * @param f the callable reference + * @param name the name of the bean + * @param scope Override the target scope of this bean, specifying a new scope name. + * @param isLazyInit Set whether this bean should be lazily initialized. + * @param isPrimary Set whether this bean is a primary autowire candidate. + * @param isAutowireCandidate Set whether this bean is a candidate for getting + * autowired into some other bean. + * @param initMethodName Set the name of the initializer method + * @param destroyMethodName Set the name of the destroy method + * @param description Set a human-readable description of this bean definition + * @param role Set the role hint for this bean definition + * @see GenericApplicationContext.registerBean + * @see org.springframework.beans.factory.config.BeanDefinition + * @since 5.2 + */ + inline fun + bean(crossinline f: (A, B, C, D, E, F, G, H, I, J, K, L, M) -> T, + name: String? = null, + scope: BeanDefinitionDsl.Scope? = null, + isLazyInit: Boolean? = null, + isPrimary: Boolean? = null, + isAutowireCandidate: Boolean? = null, + initMethodName: String? = null, + destroyMethodName: String? = null, + description: String? = null, + role: BeanDefinitionDsl.Role? = null) { + + bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) { + f.invoke(ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref()) + } + } + + /** + * Declare a bean definition using the given callable reference with 14 parameters + * autowired by type for obtaining a new instance. + * + * @param f the callable reference + * @param name the name of the bean + * @param scope Override the target scope of this bean, specifying a new scope name. + * @param isLazyInit Set whether this bean should be lazily initialized. + * @param isPrimary Set whether this bean is a primary autowire candidate. + * @param isAutowireCandidate Set whether this bean is a candidate for getting + * autowired into some other bean. + * @param initMethodName Set the name of the initializer method + * @param destroyMethodName Set the name of the destroy method + * @param description Set a human-readable description of this bean definition + * @param role Set the role hint for this bean definition + * @see GenericApplicationContext.registerBean + * @see org.springframework.beans.factory.config.BeanDefinition + * @since 5.2 + */ + inline fun + bean(crossinline f: (A, B, C, D, E, F, G, H, I, J, K, L, M, N) -> T, + name: String? = null, + scope: BeanDefinitionDsl.Scope? = null, + isLazyInit: Boolean? = null, + isPrimary: Boolean? = null, + isAutowireCandidate: Boolean? = null, + initMethodName: String? = null, + destroyMethodName: String? = null, + description: String? = null, + role: BeanDefinitionDsl.Role? = null) { + + bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) { + f.invoke(ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref()) + } + } + + /** + * Declare a bean definition using the given callable reference with 15 parameters + * autowired by type for obtaining a new instance. + * + * @param f the callable reference + * @param name the name of the bean + * @param scope Override the target scope of this bean, specifying a new scope name. + * @param isLazyInit Set whether this bean should be lazily initialized. + * @param isPrimary Set whether this bean is a primary autowire candidate. + * @param isAutowireCandidate Set whether this bean is a candidate for getting + * autowired into some other bean. + * @param initMethodName Set the name of the initializer method + * @param destroyMethodName Set the name of the destroy method + * @param description Set a human-readable description of this bean definition + * @param role Set the role hint for this bean definition + * @see GenericApplicationContext.registerBean + * @see org.springframework.beans.factory.config.BeanDefinition + * @since 5.2 + */ + inline fun + bean(crossinline f: (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O) -> T, + name: String? = null, + scope: BeanDefinitionDsl.Scope? = null, + isLazyInit: Boolean? = null, + isPrimary: Boolean? = null, + isAutowireCandidate: Boolean? = null, + initMethodName: String? = null, + destroyMethodName: String? = null, + description: String? = null, + role: BeanDefinitionDsl.Role? = null) { + + bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) { + f.invoke(ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref()) + } + } + + /** + * Declare a bean definition using the given callable reference with 16 parameters + * autowired by type for obtaining a new instance. + * + * @param f the callable reference + * @param name the name of the bean + * @param scope Override the target scope of this bean, specifying a new scope name. + * @param isLazyInit Set whether this bean should be lazily initialized. + * @param isPrimary Set whether this bean is a primary autowire candidate. + * @param isAutowireCandidate Set whether this bean is a candidate for getting + * autowired into some other bean. + * @param initMethodName Set the name of the initializer method + * @param destroyMethodName Set the name of the destroy method + * @param description Set a human-readable description of this bean definition + * @param role Set the role hint for this bean definition + * @see GenericApplicationContext.registerBean + * @see org.springframework.beans.factory.config.BeanDefinition + * @since 5.2 + */ + inline fun + bean(crossinline f: (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P) -> T, + name: String? = null, + scope: BeanDefinitionDsl.Scope? = null, + isLazyInit: Boolean? = null, + isPrimary: Boolean? = null, + isAutowireCandidate: Boolean? = null, + initMethodName: String? = null, + destroyMethodName: String? = null, + description: String? = null, + role: BeanDefinitionDsl.Role? = null) { + + bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) { + f.invoke(ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref()) + } + } + + /** + * Declare a bean definition using the given callable reference with 17 parameters + * autowired by type for obtaining a new instance. + * + * @param f the callable reference + * @param name the name of the bean + * @param scope Override the target scope of this bean, specifying a new scope name. + * @param isLazyInit Set whether this bean should be lazily initialized. + * @param isPrimary Set whether this bean is a primary autowire candidate. + * @param isAutowireCandidate Set whether this bean is a candidate for getting + * autowired into some other bean. + * @param initMethodName Set the name of the initializer method + * @param destroyMethodName Set the name of the destroy method + * @param description Set a human-readable description of this bean definition + * @param role Set the role hint for this bean definition + * @see GenericApplicationContext.registerBean + * @see org.springframework.beans.factory.config.BeanDefinition + * @since 5.2 + */ + inline fun + bean(crossinline f: (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q) -> T, + name: String? = null, + scope: BeanDefinitionDsl.Scope? = null, + isLazyInit: Boolean? = null, + isPrimary: Boolean? = null, + isAutowireCandidate: Boolean? = null, + initMethodName: String? = null, + destroyMethodName: String? = null, + description: String? = null, + role: BeanDefinitionDsl.Role? = null) { + + bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) { + f.invoke(ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref()) + } + } + + /** + * Declare a bean definition using the given callable reference with 18 parameters + * autowired by type for obtaining a new instance. + * + * @param f the callable reference + * @param name the name of the bean + * @param scope Override the target scope of this bean, specifying a new scope name. + * @param isLazyInit Set whether this bean should be lazily initialized. + * @param isPrimary Set whether this bean is a primary autowire candidate. + * @param isAutowireCandidate Set whether this bean is a candidate for getting + * autowired into some other bean. + * @param initMethodName Set the name of the initializer method + * @param destroyMethodName Set the name of the destroy method + * @param description Set a human-readable description of this bean definition + * @param role Set the role hint for this bean definition + * @see GenericApplicationContext.registerBean + * @see org.springframework.beans.factory.config.BeanDefinition + * @since 5.2 + */ + inline fun + bean(crossinline f: (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R) -> T, + name: String? = null, + scope: BeanDefinitionDsl.Scope? = null, + isLazyInit: Boolean? = null, + isPrimary: Boolean? = null, + isAutowireCandidate: Boolean? = null, + initMethodName: String? = null, + destroyMethodName: String? = null, + description: String? = null, + role: BeanDefinitionDsl.Role? = null) { + + bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) { + f.invoke(ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref()) + } + } + + /** + * Declare a bean definition using the given callable reference with 19 parameters + * autowired by type for obtaining a new instance. + * + * @param f the callable reference + * @param name the name of the bean + * @param scope Override the target scope of this bean, specifying a new scope name. + * @param isLazyInit Set whether this bean should be lazily initialized. + * @param isPrimary Set whether this bean is a primary autowire candidate. + * @param isAutowireCandidate Set whether this bean is a candidate for getting + * autowired into some other bean. + * @param initMethodName Set the name of the initializer method + * @param destroyMethodName Set the name of the destroy method + * @param description Set a human-readable description of this bean definition + * @param role Set the role hint for this bean definition + * @see GenericApplicationContext.registerBean + * @see org.springframework.beans.factory.config.BeanDefinition + * @since 5.2 + */ + inline fun + bean(crossinline f: (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S) -> T, + name: String? = null, + scope: BeanDefinitionDsl.Scope? = null, + isLazyInit: Boolean? = null, + isPrimary: Boolean? = null, + isAutowireCandidate: Boolean? = null, + initMethodName: String? = null, + destroyMethodName: String? = null, + description: String? = null, + role: BeanDefinitionDsl.Role? = null) { + + bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) { + f.invoke(ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref()) + } + } + + /** + * Declare a bean definition using the given callable reference with 20 parameters + * autowired by type for obtaining a new instance. + * + * @param f the callable reference + * @param name the name of the bean + * @param scope Override the target scope of this bean, specifying a new scope name. + * @param isLazyInit Set whether this bean should be lazily initialized. + * @param isPrimary Set whether this bean is a primary autowire candidate. + * @param isAutowireCandidate Set whether this bean is a candidate for getting + * autowired into some other bean. + * @param initMethodName Set the name of the initializer method + * @param destroyMethodName Set the name of the destroy method + * @param description Set a human-readable description of this bean definition + * @param role Set the role hint for this bean definition + * @see GenericApplicationContext.registerBean + * @see org.springframework.beans.factory.config.BeanDefinition + * @since 5.2 + */ + inline fun + bean(crossinline f: (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, U) -> T, + name: String? = null, + scope: BeanDefinitionDsl.Scope? = null, + isLazyInit: Boolean? = null, + isPrimary: Boolean? = null, + isAutowireCandidate: Boolean? = null, + initMethodName: String? = null, + destroyMethodName: String? = null, + description: String? = null, + role: BeanDefinitionDsl.Role? = null) { + + bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) { + f.invoke(ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref()) + } + } + + /** + * Declare a bean definition using the given callable reference with 21 parameters + * autowired by type for obtaining a new instance. + * + * @param f the callable reference + * @param name the name of the bean + * @param scope Override the target scope of this bean, specifying a new scope name. + * @param isLazyInit Set whether this bean should be lazily initialized. + * @param isPrimary Set whether this bean is a primary autowire candidate. + * @param isAutowireCandidate Set whether this bean is a candidate for getting + * autowired into some other bean. + * @param initMethodName Set the name of the initializer method + * @param destroyMethodName Set the name of the destroy method + * @param description Set a human-readable description of this bean definition + * @param role Set the role hint for this bean definition + * @see GenericApplicationContext.registerBean + * @see org.springframework.beans.factory.config.BeanDefinition + * @since 5.2 + */ + inline fun + bean(crossinline f: (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, U, V) -> T, + name: String? = null, + scope: BeanDefinitionDsl.Scope? = null, + isLazyInit: Boolean? = null, + isPrimary: Boolean? = null, + isAutowireCandidate: Boolean? = null, + initMethodName: String? = null, + destroyMethodName: String? = null, + description: String? = null, + role: BeanDefinitionDsl.Role? = null) { + + bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) { + f.invoke(ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref()) + } + } + + /** + * Declare a bean definition using the given callable reference with 22 parameters + * autowired by type for obtaining a new instance. + * + * @param f the callable reference + * @param name the name of the bean + * @param scope Override the target scope of this bean, specifying a new scope name. + * @param isLazyInit Set whether this bean should be lazily initialized. + * @param isPrimary Set whether this bean is a primary autowire candidate. + * @param isAutowireCandidate Set whether this bean is a candidate for getting + * autowired into some other bean. + * @param initMethodName Set the name of the initializer method + * @param destroyMethodName Set the name of the destroy method + * @param description Set a human-readable description of this bean definition + * @param role Set the role hint for this bean definition + * @see GenericApplicationContext.registerBean + * @see org.springframework.beans.factory.config.BeanDefinition + * @since 5.2 + */ + inline fun + bean(crossinline f: (A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, U, V, W) -> T, + name: String? = null, + scope: BeanDefinitionDsl.Scope? = null, + isLazyInit: Boolean? = null, + isPrimary: Boolean? = null, + isAutowireCandidate: Boolean? = null, + initMethodName: String? = null, + destroyMethodName: String? = null, + description: String? = null, + role: BeanDefinitionDsl.Role? = null) { + + bean(name, scope, isLazyInit, isPrimary, isAutowireCandidate, initMethodName, destroyMethodName, description, role) { + f.invoke(ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref(), ref()) + } + } + /** * Limit access to `ref()` and `provider()` to bean supplier lambdas. * @since 5.2 diff --git a/spring-context/src/test/kotlin/org/springframework/context/support/BeanDefinitionDslTests.kt b/spring-context/src/test/kotlin/org/springframework/context/support/BeanDefinitionDslTests.kt index c50a4ae1340..870511e6ad3 100644 --- a/spring-context/src/test/kotlin/org/springframework/context/support/BeanDefinitionDslTests.kt +++ b/spring-context/src/test/kotlin/org/springframework/context/support/BeanDefinitionDslTests.kt @@ -156,6 +156,19 @@ class BeanDefinitionDslTests { } context.getBean() } + + @Test // gh-21845 + fun `Declare beans leveraging callable reference`() { + val beans = beans { + bean() + bean(::baz) + } + val context = GenericApplicationContext().apply { + beans.initialize(this) + refresh() + } + context.getBean() + } } @@ -164,3 +177,5 @@ class Bar class Baz(val bar: Bar) class FooFoo(val name: String) class BarBar(val foos: Collection) + +fun baz(bar: Bar) = Baz(bar) diff --git a/src/docs/asciidoc/languages/kotlin.adoc b/src/docs/asciidoc/languages/kotlin.adoc index 53f5e97d7f8..f9c77fb9c44 100644 --- a/src/docs/asciidoc/languages/kotlin.adoc +++ b/src/docs/asciidoc/languages/kotlin.adoc @@ -180,10 +180,18 @@ In Java, you can, for example, write the following: [source,java,indent=0] ---- + class Foo {} + + class Bar { + private final Foo foo; + public Bar(Foo foo) { + this.foo = foo; + } + } + GenericApplicationContext context = new GenericApplicationContext(); context.registerBean(Foo.class); - context.registerBean(Bar.class, () -> new Bar(context.getBean(Foo.class)) -); + context.registerBean(Bar.class, () -> new Bar(context.getBean(Foo.class))); ---- In Kotlin, with reified type parameters and `GenericApplicationContext` Kotlin extensions, @@ -191,6 +199,10 @@ you can instead write the following: [source,kotlin,indent=0] ---- + class Foo + + class Bar(private val foo: Foo) + val context = GenericApplicationContext().apply { registerBean() registerBean { Bar(it.getBean()) } @@ -198,7 +210,7 @@ you can instead write the following: ---- ==== -If the class `Bar` has a single constructor, you can even just specify the bean class, +When the class `Bar` has a single constructor, you can even just specify the bean class, the constructor parameters will be autowired by type: ==== @@ -214,10 +226,21 @@ In order to allow a more declarative approach and cleaner syntax, Spring Framewo a {doc-root}/spring-framework/docs/{spring-version}/kdoc-api/spring-framework/org.springframework.context.support/-bean-definition-dsl/[Kotlin bean definition DSL] It declares an `ApplicationContextInitializer` through a clean declarative API, which lets you deal with profiles and `Environment` for customizing -how beans are registered. The following example creates a `Play` profile: +how beans are registered. + +In the following example notice that: + - Type inference usually allows to avoid specifying the type for bean references like `ref("bazBean")` + - It is possible to use Kotlin top level functions to declare beans using callable references like `bean(::myRouter)` in this example + - When specifying `bean()` or `bean(::myRouter)`, parameters are autowired by type + - The `FooBar` bean will be registered only if the `foobar` profile is active [source,kotlin,indent=0] ---- + class Foo + class Bar(private val foo: Foo) + class Baz(var message: String = "") + class FooBar(private val baz: Baz) + val myBeans = beans { bean() bean() @@ -229,6 +252,11 @@ how beans are registered. The following example creates a `Play` profile: profile("foobar") { bean { FooBar(ref("bazBean")) } } + bean(::myRouter) + } + + fun myRouter(foo: Foo, bar: Bar, baz: Baz) = router { + // ... } ---- @@ -275,7 +303,7 @@ and idiomatic Kotlin code to build a `RouterFunction` instance as the following [source,kotlin,indent=0] ---- - val routes: RouterFunction = router { + val myRouter: RouterFunction = router { accept(TEXT_HTML).nest { GET("/") { ok().render("index") } GET("/sse") { ok().render("sse") }