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
This commit is contained in:
parent
b6b880ce27
commit
47d6e91227
|
@ -18,7 +18,6 @@ package org.springframework.context.support
|
||||||
|
|
||||||
import org.springframework.beans.factory.config.BeanDefinition
|
import org.springframework.beans.factory.config.BeanDefinition
|
||||||
import org.springframework.beans.factory.config.BeanDefinitionCustomizer
|
import org.springframework.beans.factory.config.BeanDefinitionCustomizer
|
||||||
import org.springframework.beans.factory.support.AbstractBeanDefinition
|
|
||||||
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils
|
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils
|
||||||
import org.springframework.context.ApplicationContextInitializer
|
import org.springframework.context.ApplicationContextInitializer
|
||||||
import org.springframework.core.env.ConfigurableEnvironment
|
import org.springframework.core.env.ConfigurableEnvironment
|
||||||
|
@ -112,38 +111,6 @@ open class BeanDefinitionDsl(private val init: BeanDefinitionDsl.() -> Unit,
|
||||||
PROTOTYPE
|
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.
|
* 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.
|
* Declare a bean definition from the given bean class which can be inferred when possible.
|
||||||
*
|
*
|
||||||
|
* <p>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 name the name of the bean
|
||||||
* @param scope Override the target scope of this bean, specifying a new scope name.
|
* @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 isLazyInit Set whether this bean should be lazily initialized.
|
||||||
* @param isPrimary Set whether this bean is a primary autowire candidate.
|
* @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
|
* @param isAutowireCandidate Set whether this bean is a candidate for getting
|
||||||
* autowired into some other bean.
|
* autowired into some other bean.
|
||||||
* @param initMethodName Set the name of the initializer method
|
* @param initMethodName Set the name of the initializer method
|
||||||
|
@ -199,7 +168,6 @@ open class BeanDefinitionDsl(private val init: BeanDefinitionDsl.() -> Unit,
|
||||||
scope: Scope? = null,
|
scope: Scope? = null,
|
||||||
isLazyInit: Boolean? = null,
|
isLazyInit: Boolean? = null,
|
||||||
isPrimary: Boolean? = null,
|
isPrimary: Boolean? = null,
|
||||||
autowireMode: Autowire = Autowire.CONSTRUCTOR,
|
|
||||||
isAutowireCandidate: Boolean? = null,
|
isAutowireCandidate: Boolean? = null,
|
||||||
initMethodName: String? = null,
|
initMethodName: String? = null,
|
||||||
destroyMethodName: String? = null,
|
destroyMethodName: String? = null,
|
||||||
|
@ -215,9 +183,6 @@ open class BeanDefinitionDsl(private val init: BeanDefinitionDsl.() -> Unit,
|
||||||
destroyMethodName?.let { bd.destroyMethodName = destroyMethodName }
|
destroyMethodName?.let { bd.destroyMethodName = destroyMethodName }
|
||||||
description?.let { bd.description = description }
|
description?.let { bd.description = description }
|
||||||
bd.role = role.ordinal
|
bd.role = role.ordinal
|
||||||
if (bd is AbstractBeanDefinition) {
|
|
||||||
bd.autowireMode = autowireMode.ordinal
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
val beanName = name ?: BeanDefinitionReaderUtils.uniqueBeanName(T::class.java.name, context);
|
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 scope Override the target scope of this bean, specifying a new scope name.
|
||||||
* @param isLazyInit Set whether this bean should be lazily initialized.
|
* @param isLazyInit Set whether this bean should be lazily initialized.
|
||||||
* @param isPrimary Set whether this bean is a primary autowire candidate.
|
* @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
|
* @param isAutowireCandidate Set whether this bean is a candidate for getting
|
||||||
* autowired into some other bean.
|
* autowired into some other bean.
|
||||||
* @param initMethodName Set the name of the initializer method
|
* @param initMethodName Set the name of the initializer method
|
||||||
|
@ -246,7 +210,6 @@ open class BeanDefinitionDsl(private val init: BeanDefinitionDsl.() -> Unit,
|
||||||
scope: Scope? = null,
|
scope: Scope? = null,
|
||||||
isLazyInit: Boolean? = null,
|
isLazyInit: Boolean? = null,
|
||||||
isPrimary: Boolean? = null,
|
isPrimary: Boolean? = null,
|
||||||
autowireMode: Autowire = Autowire.NO,
|
|
||||||
isAutowireCandidate: Boolean? = null,
|
isAutowireCandidate: Boolean? = null,
|
||||||
initMethodName: String? = null,
|
initMethodName: String? = null,
|
||||||
destroyMethodName: String? = null,
|
destroyMethodName: String? = null,
|
||||||
|
@ -263,9 +226,6 @@ open class BeanDefinitionDsl(private val init: BeanDefinitionDsl.() -> Unit,
|
||||||
destroyMethodName?.let { bd.destroyMethodName = destroyMethodName }
|
destroyMethodName?.let { bd.destroyMethodName = destroyMethodName }
|
||||||
description?.let { bd.description = description }
|
description?.let { bd.description = description }
|
||||||
bd.role = role.ordinal
|
bd.role = role.ordinal
|
||||||
if (bd is AbstractBeanDefinition) {
|
|
||||||
bd.autowireMode = autowireMode.ordinal
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -143,6 +143,19 @@ class BeanDefinitionDslTests {
|
||||||
val barbar = context.getBean<BarBar>()
|
val barbar = context.getBean<BarBar>()
|
||||||
assertEquals(2, barbar.foos.size)
|
assertEquals(2, barbar.foos.size)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test // SPR-17292
|
||||||
|
fun `Declare beans leveraging constructor injection`() {
|
||||||
|
val beans = beans {
|
||||||
|
bean<Bar>()
|
||||||
|
bean<Baz>()
|
||||||
|
}
|
||||||
|
val context = GenericApplicationContext().apply {
|
||||||
|
beans.initialize(this)
|
||||||
|
refresh()
|
||||||
|
}
|
||||||
|
context.getBean<Baz>()
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue