diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java index 92fc4f2b20..638cfd8f00 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java @@ -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) { diff --git a/spring-beans/src/test/kotlin/org/springframework/beans/factory/annotation/KotlinAutowiredTests.kt b/spring-beans/src/test/kotlin/org/springframework/beans/factory/annotation/KotlinAutowiredTests.kt index 6319fa0878..a195bc1ca1 100644 --- a/spring-beans/src/test/kotlin/org/springframework/beans/factory/annotation/KotlinAutowiredTests.kt +++ b/spring-beans/src/test/kotlin/org/springframework/beans/factory/annotation/KotlinAutowiredTests.kt @@ -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