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
This commit is contained in:
sdeleuze 2018-01-10 18:11:16 +01:00
parent 5adaa1030c
commit 19a1477228
2 changed files with 22 additions and 2 deletions

View File

@ -347,7 +347,8 @@ public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBean
else if (rawCandidates.length == 1 && rawCandidates[0].getParameterCount() > 0) {
candidateConstructors = new Constructor<?>[] {rawCandidates[0]};
}
else if (nonSyntheticConstructors == 2 && primaryConstructor != null && defaultConstructor != null) {
else if (nonSyntheticConstructors == 2 && primaryConstructor != null
&& defaultConstructor != null && !primaryConstructor.equals(defaultConstructor)) {
candidateConstructors = new Constructor<?>[] {primaryConstructor, defaultConstructor};
}
else if (nonSyntheticConstructors == 1 && primaryConstructor != null) {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -169,6 +169,20 @@ class KotlinAutowiredTests {
assertEquals(tb, kb.testBean)
}
@Test // SPR-16289
fun `Instantiation via secondary constructor when a default primary is defined`() {
val bf = DefaultListableBeanFactory()
val bpp = AutowiredAnnotationBeanPostProcessor()
bpp.setBeanFactory(bf)
bf.addBeanPostProcessor(bpp)
val bd = RootBeanDefinition(KotlinBeanWithPrimaryAndSecondaryConstructors::class.java)
bd.scope = RootBeanDefinition.SCOPE_PROTOTYPE
bf.registerBeanDefinition("bean", bd)
bf.getBean(KotlinBeanWithPrimaryAndSecondaryConstructors::class.java, "foo")
bf.getBean(KotlinBeanWithPrimaryAndSecondaryConstructors::class.java)
}
@Test(expected = BeanCreationException::class) // SPR-16022
fun `No autowiring with primary and secondary non annotated constructors`() {
val bf = DefaultListableBeanFactory()
@ -244,6 +258,11 @@ class KotlinAutowiredTests {
constructor() : this(TestBean())
}
@Suppress("unused", "UNUSED_PARAMETER")
class KotlinBeanWithPrimaryAndSecondaryConstructors() {
constructor(p: String) : this()
}
class KotlinBeanWithSecondaryConstructor(
val optional: String = "foo",
val injectedFromConstructor: TestBean