16
16
17
17
package org .springframework .context .annotation ;
18
18
19
+ import java .util .Collections ;
19
20
import java .util .Map ;
21
+ import java .util .Set ;
20
22
import java .util .regex .Pattern ;
21
23
22
24
import org .junit .jupiter .api .Test ;
@@ -372,8 +374,8 @@ void individualBeanWithFactoryBeanSupplier() {
372
374
context .registerBean ("fb" , NonInstantiatedFactoryBean .class , NonInstantiatedFactoryBean ::new , bd -> bd .setLazyInit (true ));
373
375
context .refresh ();
374
376
375
- assertThat (context .getType ("fb" )).isEqualTo (String .class );
376
377
assertThat (context .getType ("&fb" )).isEqualTo (NonInstantiatedFactoryBean .class );
378
+ assertThat (context .getType ("fb" )).isEqualTo (String .class );
377
379
assertThat (context .getBeanNamesForType (FactoryBean .class )).hasSize (1 );
378
380
assertThat (context .getBeanNamesForType (NonInstantiatedFactoryBean .class )).hasSize (1 );
379
381
}
@@ -388,25 +390,55 @@ void individualBeanWithFactoryBeanSupplierAndTargetType() {
388
390
context .registerBeanDefinition ("fb" , bd );
389
391
context .refresh ();
390
392
391
- assertThat (context .getType ("fb" )).isEqualTo (String .class );
392
393
assertThat (context .getType ("&fb" )).isEqualTo (FactoryBean .class );
394
+ assertThat (context .getType ("fb" )).isEqualTo (String .class );
393
395
assertThat (context .getBeanNamesForType (FactoryBean .class )).hasSize (1 );
394
396
assertThat (context .getBeanNamesForType (NonInstantiatedFactoryBean .class )).isEmpty ();
395
397
}
396
398
399
+ @ Test
400
+ void individualBeanWithFactoryBeanTypeAsTargetType () {
401
+ AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext ();
402
+ RootBeanDefinition bd1 = new RootBeanDefinition ();
403
+ bd1 .setBeanClass (SetFactoryBean .class );
404
+ bd1 .setTargetType (ResolvableType .forClassWithGenerics (FactoryBean .class , ResolvableType .forClassWithGenerics (Set .class , String .class )));
405
+ bd1 .setLazyInit (true );
406
+ context .registerBeanDefinition ("fb1" , bd1 );
407
+ RootBeanDefinition bd2 = new RootBeanDefinition ();
408
+ bd2 .setBeanClass (UntypedFactoryBean .class );
409
+ bd2 .setTargetType (ResolvableType .forClassWithGenerics (FactoryBean .class , ResolvableType .forClassWithGenerics (Set .class , Integer .class )));
410
+ bd2 .setLazyInit (true );
411
+ context .registerBeanDefinition ("fb2" , bd2 );
412
+ context .registerBeanDefinition ("ip" , new RootBeanDefinition (FactoryBeanInjectionPoints .class ));
413
+ context .refresh ();
414
+
415
+ assertThat (context .getType ("&fb1" )).isEqualTo (SetFactoryBean .class );
416
+ assertThat (context .getType ("fb1" )).isEqualTo (Set .class );
417
+ assertThat (context .getBeanNamesForType (FactoryBean .class )).hasSize (2 );
418
+ assertThat (context .getBeanNamesForType (SetFactoryBean .class )).hasSize (1 );
419
+ assertThat (context .getBean ("ip" , FactoryBeanInjectionPoints .class ).factoryBean ).isSameAs (context .getBean ("&fb1" ));
420
+ assertThat (context .getBean ("ip" , FactoryBeanInjectionPoints .class ).factoryResult ).isSameAs (context .getBean ("fb1" ));
421
+ }
422
+
397
423
@ Test
398
424
void individualBeanWithFactoryBeanObjectTypeAsTargetType () {
399
425
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext ();
400
- RootBeanDefinition bd = new RootBeanDefinition ();
401
- bd .setBeanClass (TypedFactoryBean .class );
402
- bd .setTargetType (String .class );
403
- context .registerBeanDefinition ("fb" , bd );
426
+ RootBeanDefinition bd1 = new RootBeanDefinition ();
427
+ bd1 .setBeanClass (SetFactoryBean .class );
428
+ bd1 .setTargetType (ResolvableType .forClassWithGenerics (Set .class , String .class ));
429
+ context .registerBeanDefinition ("fb1" , bd1 );
430
+ RootBeanDefinition bd2 = new RootBeanDefinition ();
431
+ bd2 .setBeanClass (UntypedFactoryBean .class );
432
+ bd2 .setTargetType (ResolvableType .forClassWithGenerics (Set .class , Integer .class ));
433
+ context .registerBeanDefinition ("fb2" , bd2 );
434
+ context .registerBeanDefinition ("ip" , new RootBeanDefinition (FactoryResultInjectionPoint .class ));
404
435
context .refresh ();
405
436
406
- assertThat (context .getType ("&fb" )).isEqualTo (TypedFactoryBean .class );
407
- assertThat (context .getType ("fb" )).isEqualTo (String .class );
408
- assertThat (context .getBeanNamesForType (FactoryBean .class )).hasSize (1 );
409
- assertThat (context .getBeanNamesForType (TypedFactoryBean .class )).hasSize (1 );
437
+ assertThat (context .getType ("&fb1" )).isEqualTo (SetFactoryBean .class );
438
+ assertThat (context .getType ("fb1" )).isEqualTo (Set .class );
439
+ assertThat (context .getBeanNamesForType (FactoryBean .class )).hasSize (2 );
440
+ assertThat (context .getBeanNamesForType (SetFactoryBean .class )).hasSize (1 );
441
+ assertThat (context .getBean ("ip" , FactoryResultInjectionPoint .class ).factoryResult ).isSameAs (context .getBean ("fb1" ));
410
442
}
411
443
412
444
@ Test
@@ -630,6 +662,36 @@ public boolean isSingleton() {
630
662
return false ;
631
663
}
632
664
}
665
+
666
+ static class SetFactoryBean implements FactoryBean <Set <String >> {
667
+
668
+ @ Override
669
+ public Set <String > getObject () {
670
+ return Collections .emptySet ();
671
+ }
672
+
673
+ @ Override
674
+ public Class <?> getObjectType () {
675
+ return Set .class ;
676
+ }
677
+
678
+ @ Override
679
+ public boolean isSingleton () {
680
+ return true ;
681
+ }
682
+ }
683
+
684
+ static class FactoryResultInjectionPoint {
685
+
686
+ @ Autowired
687
+ Set <String > factoryResult ;
688
+ }
689
+
690
+ static class FactoryBeanInjectionPoints extends FactoryResultInjectionPoint {
691
+
692
+ @ Autowired
693
+ FactoryBean <Set <String >> factoryBean ;
694
+ }
633
695
}
634
696
635
697
class TestBean {
0 commit comments