18
18
19
19
import java .lang .annotation .Retention ;
20
20
import java .lang .annotation .RetentionPolicy ;
21
+ import java .util .Optional ;
21
22
22
23
import org .junit .jupiter .api .Test ;
23
24
@@ -85,20 +86,26 @@ void scopedProxy() {
85
86
@ Test
86
87
void primary () {
87
88
AnnotationConfigApplicationContext ctx =
88
- new AnnotationConfigApplicationContext (PrimaryConfig .class , StandardPojo .class );
89
+ new AnnotationConfigApplicationContext (PrimaryConfig .class , StandardPojo .class , ConstructorPojo . class );
89
90
StandardPojo pojo = ctx .getBean (StandardPojo .class );
90
91
assertThat (pojo .testBean .getName ()).isEqualTo ("interesting" );
91
92
assertThat (pojo .testBean2 .getName ()).isEqualTo ("boring" );
93
+ ConstructorPojo pojo2 = ctx .getBean (ConstructorPojo .class );
94
+ assertThat (pojo2 .testBean .getName ()).isEqualTo ("interesting" );
95
+ assertThat (pojo2 .testBean2 .getName ()).isEqualTo ("boring" );
92
96
ctx .close ();
93
97
}
94
98
95
99
@ Test
96
100
void fallback () {
97
101
AnnotationConfigApplicationContext ctx =
98
- new AnnotationConfigApplicationContext (FallbackConfig .class , StandardPojo .class );
102
+ new AnnotationConfigApplicationContext (FallbackConfig .class , StandardPojo .class , ConstructorPojo . class );
99
103
StandardPojo pojo = ctx .getBean (StandardPojo .class );
100
104
assertThat (pojo .testBean .getName ()).isEqualTo ("interesting" );
101
105
assertThat (pojo .testBean2 .getName ()).isEqualTo ("boring" );
106
+ ConstructorPojo pojo2 = ctx .getBean (ConstructorPojo .class );
107
+ assertThat (pojo2 .testBean .getName ()).isEqualTo ("interesting" );
108
+ assertThat (pojo2 .testBean2 .getName ()).isEqualTo ("boring" );
102
109
ctx .close ();
103
110
}
104
111
@@ -233,7 +240,7 @@ public static TestBean testBean1() {
233
240
234
241
@ Bean @ Qualifier ("interesting" )
235
242
public static TestBean testBean1x () {
236
- return new TestBean ("interesting " );
243
+ return new TestBean ("" );
237
244
}
238
245
239
246
@ Bean @ Boring @ Primary
@@ -245,7 +252,7 @@ public TestBean testBean2(TestBean testBean1) {
245
252
246
253
@ Bean @ Boring
247
254
public TestBean testBean2x () {
248
- return new TestBean ("boring " );
255
+ return new TestBean ("" );
249
256
}
250
257
}
251
258
@@ -259,7 +266,7 @@ public static TestBean testBean1() {
259
266
260
267
@ Bean @ Qualifier ("interesting" ) @ Fallback
261
268
public static TestBean testBean1x () {
262
- return new TestBean ("interesting " );
269
+ return new TestBean ("" );
263
270
}
264
271
265
272
@ Bean @ Boring
@@ -271,7 +278,7 @@ public TestBean testBean2(TestBean testBean1) {
271
278
272
279
@ Bean @ Boring @ Fallback
273
280
public TestBean testBean2x () {
274
- return new TestBean ("boring " );
281
+ return new TestBean ("" );
275
282
}
276
283
}
277
284
@@ -283,6 +290,19 @@ static class StandardPojo {
283
290
@ Autowired @ Boring TestBean testBean2 ;
284
291
}
285
292
293
+ @ Component @ Lazy
294
+ static class ConstructorPojo {
295
+
296
+ TestBean testBean ;
297
+
298
+ TestBean testBean2 ;
299
+
300
+ ConstructorPojo (@ Qualifier ("interesting" ) TestBean testBean , @ Boring TestBean testBean2 ) {
301
+ this .testBean = testBean ;
302
+ this .testBean2 = testBean2 ;
303
+ }
304
+ }
305
+
286
306
@ Qualifier
287
307
@ Retention (RetentionPolicy .RUNTIME )
288
308
public @interface Boring {
@@ -323,11 +343,15 @@ public TestBean testBean2(@Lazy TestBean testBean1) {
323
343
@ InterestingPojo
324
344
static class CustomPojo {
325
345
326
- @ Autowired ( required = false ) TestBean plainBean ;
346
+ TestBean plainBean ;
327
347
328
348
@ InterestingNeed TestBean testBean ;
329
349
330
350
@ InterestingNeedWithRequiredOverride (required =false ) NestedTestBean nestedTestBean ;
351
+
352
+ public CustomPojo (Optional <TestBean > plainBean ) {
353
+ this .plainBean = plainBean .orElse (null );
354
+ }
331
355
}
332
356
333
357
@ Bean (defaultCandidate =false ) @ Lazy @ Qualifier ("interesting" )
0 commit comments