@@ -122,6 +122,36 @@ class KotlinAutowiredTests {
122
122
assertSame(colour, kb.injectedFromSecondaryConstructor)
123
123
}
124
124
125
+ @Test // SPR-16012
126
+ fun `Fallback on the default constructor when no autowirable primary constructor is defined` () {
127
+ val bf = DefaultListableBeanFactory ()
128
+ val bpp = AutowiredAnnotationBeanPostProcessor ()
129
+ bpp.setBeanFactory(bf)
130
+ bf.addBeanPostProcessor(bpp)
131
+ val bd = RootBeanDefinition (KotlinBeanWithPrimaryAndDefaultConstructors ::class .java)
132
+ bd.scope = RootBeanDefinition .SCOPE_PROTOTYPE
133
+ bf.registerBeanDefinition(" bean" , bd)
134
+
135
+ val kb = bf.getBean(" bean" , KotlinBeanWithPrimaryAndDefaultConstructors ::class .java)
136
+ assertNotNull(kb.testBean)
137
+ }
138
+
139
+ @Test // SPR-16012
140
+ fun `Instantiation via primary constructor when a default is defined` () {
141
+ val bf = DefaultListableBeanFactory ()
142
+ val bpp = AutowiredAnnotationBeanPostProcessor ()
143
+ bpp.setBeanFactory(bf)
144
+ bf.addBeanPostProcessor(bpp)
145
+ val bd = RootBeanDefinition (KotlinBeanWithPrimaryAndDefaultConstructors ::class .java)
146
+ bd.scope = RootBeanDefinition .SCOPE_PROTOTYPE
147
+ bf.registerBeanDefinition(" bean" , bd)
148
+ val tb = TestBean ()
149
+ bf.registerSingleton(" testBean" , tb)
150
+
151
+ val kb = bf.getBean(" bean" , KotlinBeanWithPrimaryAndDefaultConstructors ::class .java)
152
+ assertEquals(tb, kb.testBean)
153
+ }
154
+
125
155
126
156
class KotlinBean (val injectedFromConstructor : TestBean ? ) {
127
157
@@ -163,4 +193,9 @@ class KotlinAutowiredTests {
163
193
var injectedFromSecondaryConstructor: Colour ? = null
164
194
}
165
195
196
+ @Suppress(" unused" )
197
+ class KotlinBeanWithPrimaryAndDefaultConstructors (val testBean : TestBean ) {
198
+ constructor () : this (TestBean ())
199
+ }
200
+
166
201
}
0 commit comments