From 47d6e9122751efc78f41632483302d365bb1a2ca Mon Sep 17 00:00:00 2001 From: Sebastien Deleuze Date: Wed, 19 Sep 2018 23:40:32 +0200 Subject: [PATCH] Leverage non-default ctor support in Kotlin bean DSL Since non-default constructors are now evaluated for autowiring, there is no need anymore for setting autowiring mode or exposing it in Kotlin bean DSL. Issue: SPR-17292 --- .../context/support/BeanDefinitionDsl.kt | 46 ++----------------- .../context/support/BeanDefinitionDslTests.kt | 13 ++++++ 2 files changed, 16 insertions(+), 43 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 6930b95b70..15326d9cea 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 @@ -18,7 +18,6 @@ package org.springframework.context.support import org.springframework.beans.factory.config.BeanDefinition import org.springframework.beans.factory.config.BeanDefinitionCustomizer -import org.springframework.beans.factory.support.AbstractBeanDefinition import org.springframework.beans.factory.support.BeanDefinitionReaderUtils import org.springframework.context.ApplicationContextInitializer import org.springframework.core.env.ConfigurableEnvironment @@ -112,38 +111,6 @@ open class BeanDefinitionDsl(private val init: BeanDefinitionDsl.() -> Unit, PROTOTYPE } - /** - * Autowire enum constants. - */ - enum class Autowire { - - /** - * Autowire constant that indicates no externally defined autowiring - * @see org.springframework.beans.factory.config.AutowireCapableBeanFactory.AUTOWIRE_NO - */ - NO, - - /** - * Autowire constant that indicates autowiring bean properties by name - * @see org.springframework.beans.factory.config.AutowireCapableBeanFactory.AUTOWIRE_BY_NAME - */ - BY_NAME, - - /** - * Autowire constant that indicates autowiring bean properties by type - * @see org.springframework.beans.factory.config.AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE - */ - BY_TYPE, - - /** - * Autowire constant that indicates autowiring the greediest constructor that can be satisfied - * @see org.springframework.beans.factory.config.AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR - */ - CONSTRUCTOR - - } - - /** * Role enum constants. */ @@ -181,11 +148,13 @@ open class BeanDefinitionDsl(private val init: BeanDefinitionDsl.() -> Unit, /** * Declare a bean definition from the given bean class which can be inferred when possible. * + *

The preferred constructor (Kotlin primary constructor and standard public constructors) + * are evaluated for autowiring before falling back to default instantiation. + * * @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 autowireMode Set the autowire mode, `Autowire.CONSTRUCTOR` by default * @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 @@ -199,7 +168,6 @@ open class BeanDefinitionDsl(private val init: BeanDefinitionDsl.() -> Unit, scope: Scope? = null, isLazyInit: Boolean? = null, isPrimary: Boolean? = null, - autowireMode: Autowire = Autowire.CONSTRUCTOR, isAutowireCandidate: Boolean? = null, initMethodName: String? = null, destroyMethodName: String? = null, @@ -215,9 +183,6 @@ open class BeanDefinitionDsl(private val init: BeanDefinitionDsl.() -> Unit, destroyMethodName?.let { bd.destroyMethodName = destroyMethodName } description?.let { bd.description = description } bd.role = role.ordinal - if (bd is AbstractBeanDefinition) { - bd.autowireMode = autowireMode.ordinal - } } val beanName = name ?: BeanDefinitionReaderUtils.uniqueBeanName(T::class.java.name, context); @@ -231,7 +196,6 @@ open class BeanDefinitionDsl(private val init: BeanDefinitionDsl.() -> Unit, * @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 autowireMode Set the autowire mode, `Autowire.NO` by default * @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 @@ -246,7 +210,6 @@ open class BeanDefinitionDsl(private val init: BeanDefinitionDsl.() -> Unit, scope: Scope? = null, isLazyInit: Boolean? = null, isPrimary: Boolean? = null, - autowireMode: Autowire = Autowire.NO, isAutowireCandidate: Boolean? = null, initMethodName: String? = null, destroyMethodName: String? = null, @@ -263,9 +226,6 @@ open class BeanDefinitionDsl(private val init: BeanDefinitionDsl.() -> Unit, destroyMethodName?.let { bd.destroyMethodName = destroyMethodName } description?.let { bd.description = description } bd.role = role.ordinal - if (bd is AbstractBeanDefinition) { - bd.autowireMode = autowireMode.ordinal - } } 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 8863788034..794e55576a 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 @@ -143,6 +143,19 @@ class BeanDefinitionDslTests { val barbar = context.getBean() assertEquals(2, barbar.foos.size) } + + @Test // SPR-17292 + fun `Declare beans leveraging constructor injection`() { + val beans = beans { + bean() + bean() + } + val context = GenericApplicationContext().apply { + beans.initialize(this) + refresh() + } + context.getBean() + } }