Skip to content

Commit 47d6e91

Browse files
committed
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
1 parent b6b880c commit 47d6e91

File tree

2 files changed

+16
-43
lines changed

2 files changed

+16
-43
lines changed

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

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package org.springframework.context.support
1818

1919
import org.springframework.beans.factory.config.BeanDefinition
2020
import org.springframework.beans.factory.config.BeanDefinitionCustomizer
21-
import org.springframework.beans.factory.support.AbstractBeanDefinition
2221
import org.springframework.beans.factory.support.BeanDefinitionReaderUtils
2322
import org.springframework.context.ApplicationContextInitializer
2423
import org.springframework.core.env.ConfigurableEnvironment
@@ -112,38 +111,6 @@ open class BeanDefinitionDsl(private val init: BeanDefinitionDsl.() -> Unit,
112111
PROTOTYPE
113112
}
114113

115-
/**
116-
* Autowire enum constants.
117-
*/
118-
enum class Autowire {
119-
120-
/**
121-
* Autowire constant that indicates no externally defined autowiring
122-
* @see org.springframework.beans.factory.config.AutowireCapableBeanFactory.AUTOWIRE_NO
123-
*/
124-
NO,
125-
126-
/**
127-
* Autowire constant that indicates autowiring bean properties by name
128-
* @see org.springframework.beans.factory.config.AutowireCapableBeanFactory.AUTOWIRE_BY_NAME
129-
*/
130-
BY_NAME,
131-
132-
/**
133-
* Autowire constant that indicates autowiring bean properties by type
134-
* @see org.springframework.beans.factory.config.AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE
135-
*/
136-
BY_TYPE,
137-
138-
/**
139-
* Autowire constant that indicates autowiring the greediest constructor that can be satisfied
140-
* @see org.springframework.beans.factory.config.AutowireCapableBeanFactory.AUTOWIRE_CONSTRUCTOR
141-
*/
142-
CONSTRUCTOR
143-
144-
}
145-
146-
147114
/**
148115
* Role enum constants.
149116
*/
@@ -181,11 +148,13 @@ open class BeanDefinitionDsl(private val init: BeanDefinitionDsl.() -> Unit,
181148
/**
182149
* Declare a bean definition from the given bean class which can be inferred when possible.
183150
*
151+
* <p>The preferred constructor (Kotlin primary constructor and standard public constructors)
152+
* are evaluated for autowiring before falling back to default instantiation.
153+
*
184154
* @param name the name of the bean
185155
* @param scope Override the target scope of this bean, specifying a new scope name.
186156
* @param isLazyInit Set whether this bean should be lazily initialized.
187157
* @param isPrimary Set whether this bean is a primary autowire candidate.
188-
* @param autowireMode Set the autowire mode, `Autowire.CONSTRUCTOR` by default
189158
* @param isAutowireCandidate Set whether this bean is a candidate for getting
190159
* autowired into some other bean.
191160
* @param initMethodName Set the name of the initializer method
@@ -199,7 +168,6 @@ open class BeanDefinitionDsl(private val init: BeanDefinitionDsl.() -> Unit,
199168
scope: Scope? = null,
200169
isLazyInit: Boolean? = null,
201170
isPrimary: Boolean? = null,
202-
autowireMode: Autowire = Autowire.CONSTRUCTOR,
203171
isAutowireCandidate: Boolean? = null,
204172
initMethodName: String? = null,
205173
destroyMethodName: String? = null,
@@ -215,9 +183,6 @@ open class BeanDefinitionDsl(private val init: BeanDefinitionDsl.() -> Unit,
215183
destroyMethodName?.let { bd.destroyMethodName = destroyMethodName }
216184
description?.let { bd.description = description }
217185
bd.role = role.ordinal
218-
if (bd is AbstractBeanDefinition) {
219-
bd.autowireMode = autowireMode.ordinal
220-
}
221186
}
222187

223188
val beanName = name ?: BeanDefinitionReaderUtils.uniqueBeanName(T::class.java.name, context);
@@ -231,7 +196,6 @@ open class BeanDefinitionDsl(private val init: BeanDefinitionDsl.() -> Unit,
231196
* @param scope Override the target scope of this bean, specifying a new scope name.
232197
* @param isLazyInit Set whether this bean should be lazily initialized.
233198
* @param isPrimary Set whether this bean is a primary autowire candidate.
234-
* @param autowireMode Set the autowire mode, `Autowire.NO` by default
235199
* @param isAutowireCandidate Set whether this bean is a candidate for getting
236200
* autowired into some other bean.
237201
* @param initMethodName Set the name of the initializer method
@@ -246,7 +210,6 @@ open class BeanDefinitionDsl(private val init: BeanDefinitionDsl.() -> Unit,
246210
scope: Scope? = null,
247211
isLazyInit: Boolean? = null,
248212
isPrimary: Boolean? = null,
249-
autowireMode: Autowire = Autowire.NO,
250213
isAutowireCandidate: Boolean? = null,
251214
initMethodName: String? = null,
252215
destroyMethodName: String? = null,
@@ -263,9 +226,6 @@ open class BeanDefinitionDsl(private val init: BeanDefinitionDsl.() -> Unit,
263226
destroyMethodName?.let { bd.destroyMethodName = destroyMethodName }
264227
description?.let { bd.description = description }
265228
bd.role = role.ordinal
266-
if (bd is AbstractBeanDefinition) {
267-
bd.autowireMode = autowireMode.ordinal
268-
}
269229
}
270230

271231

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,19 @@ class BeanDefinitionDslTests {
143143
val barbar = context.getBean<BarBar>()
144144
assertEquals(2, barbar.foos.size)
145145
}
146+
147+
@Test // SPR-17292
148+
fun `Declare beans leveraging constructor injection`() {
149+
val beans = beans {
150+
bean<Bar>()
151+
bean<Baz>()
152+
}
153+
val context = GenericApplicationContext().apply {
154+
beans.initialize(this)
155+
refresh()
156+
}
157+
context.getBean<Baz>()
158+
}
146159

147160
}
148161

0 commit comments

Comments
 (0)