Skip to content

Commit 19a1477

Browse files
committed
Fix Kotlin bean w/ default + secondary ctors handling
This commit polishes SPR-16022 fix in order to handle correctly the case when primary and default constructors are the same when a secondary constructor is defined. Issue: SPR-16289
1 parent 5adaa10 commit 19a1477

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,8 @@ else if (candidates.size() == 1 && logger.isWarnEnabled()) {
347347
else if (rawCandidates.length == 1 && rawCandidates[0].getParameterCount() > 0) {
348348
candidateConstructors = new Constructor<?>[] {rawCandidates[0]};
349349
}
350-
else if (nonSyntheticConstructors == 2 && primaryConstructor != null && defaultConstructor != null) {
350+
else if (nonSyntheticConstructors == 2 && primaryConstructor != null
351+
&& defaultConstructor != null && !primaryConstructor.equals(defaultConstructor)) {
351352
candidateConstructors = new Constructor<?>[] {primaryConstructor, defaultConstructor};
352353
}
353354
else if (nonSyntheticConstructors == 1 && primaryConstructor != null) {

spring-beans/src/test/kotlin/org/springframework/beans/factory/annotation/KotlinAutowiredTests.kt

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -169,6 +169,20 @@ class KotlinAutowiredTests {
169169
assertEquals(tb, kb.testBean)
170170
}
171171

172+
@Test // SPR-16289
173+
fun `Instantiation via secondary constructor when a default primary is defined`() {
174+
val bf = DefaultListableBeanFactory()
175+
val bpp = AutowiredAnnotationBeanPostProcessor()
176+
bpp.setBeanFactory(bf)
177+
bf.addBeanPostProcessor(bpp)
178+
val bd = RootBeanDefinition(KotlinBeanWithPrimaryAndSecondaryConstructors::class.java)
179+
bd.scope = RootBeanDefinition.SCOPE_PROTOTYPE
180+
bf.registerBeanDefinition("bean", bd)
181+
182+
bf.getBean(KotlinBeanWithPrimaryAndSecondaryConstructors::class.java, "foo")
183+
bf.getBean(KotlinBeanWithPrimaryAndSecondaryConstructors::class.java)
184+
}
185+
172186
@Test(expected = BeanCreationException::class) // SPR-16022
173187
fun `No autowiring with primary and secondary non annotated constructors`() {
174188
val bf = DefaultListableBeanFactory()
@@ -244,6 +258,11 @@ class KotlinAutowiredTests {
244258
constructor() : this(TestBean())
245259
}
246260

261+
@Suppress("unused", "UNUSED_PARAMETER")
262+
class KotlinBeanWithPrimaryAndSecondaryConstructors() {
263+
constructor(p: String) : this()
264+
}
265+
247266
class KotlinBeanWithSecondaryConstructor(
248267
val optional: String = "foo",
249268
val injectedFromConstructor: TestBean

0 commit comments

Comments
 (0)