4343import org .springframework .beans .factory .config .TypedStringValue ;
4444import org .springframework .beans .factory .xml .XmlBeanDefinitionReader ;
4545import org .springframework .beans .propertyeditors .CustomNumberEditor ;
46- import org .springframework .core .Ordered ;
4746import org .springframework .core .OverridingClassLoader ;
4847import org .springframework .core .ResolvableType ;
48+ import org .springframework .core .annotation .AnnotationAwareOrderComparator ;
49+ import org .springframework .core .annotation .Order ;
4950import org .springframework .core .io .ClassPathResource ;
5051import org .springframework .core .io .UrlResource ;
5152import org .springframework .tests .Assume ;
@@ -83,7 +84,7 @@ public void testGenericSetProperty() {
8384 }
8485
8586 @ Test
86- public void testGenericListProperty () throws MalformedURLException {
87+ public void testGenericListProperty () throws Exception {
8788 DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
8889 RootBeanDefinition rbd = new RootBeanDefinition (GenericBean .class );
8990
@@ -100,7 +101,7 @@ public void testGenericListProperty() throws MalformedURLException {
100101 }
101102
102103 @ Test
103- public void testGenericListPropertyWithAutowiring () throws MalformedURLException {
104+ public void testGenericListPropertyWithAutowiring () throws Exception {
104105 DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
105106 bf .registerSingleton ("resource1" , new UrlResource ("http://localhost:8080" ));
106107 bf .registerSingleton ("resource2" , new UrlResource ("http://localhost:9090" ));
@@ -223,7 +224,7 @@ public void testGenericSetConstructorWithOptionalAutowiring() {
223224 }
224225
225226 @ Test
226- public void testGenericSetListConstructor () throws MalformedURLException {
227+ public void testGenericSetListConstructor () throws Exception {
227228 DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
228229 RootBeanDefinition rbd = new RootBeanDefinition (GenericBean .class );
229230
@@ -246,7 +247,7 @@ public void testGenericSetListConstructor() throws MalformedURLException {
246247 }
247248
248249 @ Test
249- public void testGenericSetListConstructorWithAutowiring () throws MalformedURLException {
250+ public void testGenericSetListConstructorWithAutowiring () throws Exception {
250251 DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
251252 bf .registerSingleton ("integer1" , new Integer (4 ));
252253 bf .registerSingleton ("integer2" , new Integer (5 ));
@@ -265,7 +266,7 @@ public void testGenericSetListConstructorWithAutowiring() throws MalformedURLExc
265266 }
266267
267268 @ Test
268- public void testGenericSetListConstructorWithOptionalAutowiring () throws MalformedURLException {
269+ public void testGenericSetListConstructorWithOptionalAutowiring () throws Exception {
269270 DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
270271 bf .registerSingleton ("resource1" , new UrlResource ("http://localhost:8080" ));
271272 bf .registerSingleton ("resource2" , new UrlResource ("http://localhost:9090" ));
@@ -280,7 +281,7 @@ public void testGenericSetListConstructorWithOptionalAutowiring() throws Malform
280281 }
281282
282283 @ Test
283- public void testGenericSetMapConstructor () throws MalformedURLException {
284+ public void testGenericSetMapConstructor () {
284285 DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
285286 RootBeanDefinition rbd = new RootBeanDefinition (GenericBean .class );
286287
@@ -303,7 +304,7 @@ public void testGenericSetMapConstructor() throws MalformedURLException {
303304 }
304305
305306 @ Test
306- public void testGenericMapResourceConstructor () throws MalformedURLException {
307+ public void testGenericMapResourceConstructor () throws Exception {
307308 DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
308309 RootBeanDefinition rbd = new RootBeanDefinition (GenericBean .class );
309310
@@ -322,7 +323,7 @@ public void testGenericMapResourceConstructor() throws MalformedURLException {
322323 }
323324
324325 @ Test
325- public void testGenericMapMapConstructor () throws MalformedURLException {
326+ public void testGenericMapMapConstructor () {
326327 DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
327328 RootBeanDefinition rbd = new RootBeanDefinition (GenericBean .class );
328329
@@ -348,7 +349,7 @@ public void testGenericMapMapConstructor() throws MalformedURLException {
348349 }
349350
350351 @ Test
351- public void testGenericMapMapConstructorWithSameRefAndConversion () throws MalformedURLException {
352+ public void testGenericMapMapConstructorWithSameRefAndConversion () {
352353 DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
353354 RootBeanDefinition rbd = new RootBeanDefinition (GenericBean .class );
354355
@@ -371,7 +372,7 @@ public void testGenericMapMapConstructorWithSameRefAndConversion() throws Malfor
371372 }
372373
373374 @ Test
374- public void testGenericMapMapConstructorWithSameRefAndNoConversion () throws MalformedURLException {
375+ public void testGenericMapMapConstructorWithSameRefAndNoConversion () {
375376 DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
376377 RootBeanDefinition rbd = new RootBeanDefinition (GenericBean .class );
377378
@@ -391,7 +392,7 @@ public void testGenericMapMapConstructorWithSameRefAndNoConversion() throws Malf
391392 }
392393
393394 @ Test
394- public void testGenericMapWithKeyTypeConstructor () throws MalformedURLException {
395+ public void testGenericMapWithKeyTypeConstructor () {
395396 DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
396397 RootBeanDefinition rbd = new RootBeanDefinition (GenericBean .class );
397398
@@ -408,7 +409,7 @@ public void testGenericMapWithKeyTypeConstructor() throws MalformedURLException
408409 }
409410
410411 @ Test
411- public void testGenericMapWithCollectionValueConstructor () throws MalformedURLException {
412+ public void testGenericMapWithCollectionValueConstructor () {
412413 DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
413414 bf .addPropertyEditorRegistrar (new PropertyEditorRegistrar () {
414415 @ Override
@@ -455,7 +456,7 @@ public void testGenericSetFactoryMethod() {
455456 }
456457
457458 @ Test
458- public void testGenericSetListFactoryMethod () throws MalformedURLException {
459+ public void testGenericSetListFactoryMethod () throws Exception {
459460 DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
460461 RootBeanDefinition rbd = new RootBeanDefinition (GenericBean .class );
461462 rbd .setFactoryMethodName ("createInstance" );
@@ -479,7 +480,7 @@ public void testGenericSetListFactoryMethod() throws MalformedURLException {
479480 }
480481
481482 @ Test
482- public void testGenericSetMapFactoryMethod () throws MalformedURLException {
483+ public void testGenericSetMapFactoryMethod () {
483484 DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
484485 RootBeanDefinition rbd = new RootBeanDefinition (GenericBean .class );
485486 rbd .setFactoryMethodName ("createInstance" );
@@ -503,7 +504,7 @@ public void testGenericSetMapFactoryMethod() throws MalformedURLException {
503504 }
504505
505506 @ Test
506- public void testGenericMapResourceFactoryMethod () throws MalformedURLException {
507+ public void testGenericMapResourceFactoryMethod () throws Exception {
507508 DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
508509 RootBeanDefinition rbd = new RootBeanDefinition (GenericBean .class );
509510 rbd .setFactoryMethodName ("createInstance" );
@@ -523,7 +524,7 @@ public void testGenericMapResourceFactoryMethod() throws MalformedURLException {
523524 }
524525
525526 @ Test
526- public void testGenericMapMapFactoryMethod () throws MalformedURLException {
527+ public void testGenericMapMapFactoryMethod () {
527528 DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
528529 RootBeanDefinition rbd = new RootBeanDefinition (GenericBean .class );
529530 rbd .setFactoryMethodName ("createInstance" );
@@ -547,7 +548,7 @@ public void testGenericMapMapFactoryMethod() throws MalformedURLException {
547548 }
548549
549550 @ Test
550- public void testGenericMapWithKeyTypeFactoryMethod () throws MalformedURLException {
551+ public void testGenericMapWithKeyTypeFactoryMethod () {
551552 DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
552553 RootBeanDefinition rbd = new RootBeanDefinition (GenericBean .class );
553554 rbd .setFactoryMethodName ("createInstance" );
@@ -565,7 +566,7 @@ public void testGenericMapWithKeyTypeFactoryMethod() throws MalformedURLExceptio
565566 }
566567
567568 @ Test
568- public void testGenericMapWithCollectionValueFactoryMethod () throws MalformedURLException {
569+ public void testGenericMapWithCollectionValueFactoryMethod () {
569570 DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
570571 bf .addPropertyEditorRegistrar (new PropertyEditorRegistrar () {
571572 @ Override
@@ -625,7 +626,7 @@ public void testGenericMapBean() throws Exception {
625626 }
626627
627628 @ Test
628- public void testGenericallyTypedIntegerBean () throws Exception {
629+ public void testGenericallyTypedIntegerBean () {
629630 DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
630631 new XmlBeanDefinitionReader (bf ).loadBeanDefinitions (
631632 new ClassPathResource ("genericBeanTests.xml" , getClass ()));
@@ -636,7 +637,7 @@ public void testGenericallyTypedIntegerBean() throws Exception {
636637 }
637638
638639 @ Test
639- public void testGenericallyTypedSetOfIntegerBean () throws Exception {
640+ public void testGenericallyTypedSetOfIntegerBean () {
640641 DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
641642 new XmlBeanDefinitionReader (bf ).loadBeanDefinitions (
642643 new ClassPathResource ("genericBeanTests.xml" , getClass ()));
@@ -846,10 +847,15 @@ public void testGenericMatchingWithBeanNameDifferentiation() {
846847 @ Test
847848 public void testGenericMatchingWithFullTypeDifferentiation () {
848849 DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
850+ bf .setDependencyComparator (AnnotationAwareOrderComparator .INSTANCE );
849851 bf .setAutowireCandidateResolver (new GenericTypeAwareAutowireCandidateResolver ());
850852
851- bf .registerBeanDefinition ("store1" , new RootBeanDefinition (DoubleStore .class ));
852- bf .registerBeanDefinition ("store2" , new RootBeanDefinition (FloatStore .class ));
853+ RootBeanDefinition bd1 = new RootBeanDefinition (NumberStoreFactory .class );
854+ bd1 .setFactoryMethodName ("newDoubleStore" );
855+ bf .registerBeanDefinition ("store1" , bd1 );
856+ RootBeanDefinition bd2 = new RootBeanDefinition (NumberStoreFactory .class );
857+ bd2 .setFactoryMethodName ("newFloatStore" );
858+ bf .registerBeanDefinition ("store2" , bd2 );
853859 bf .registerBeanDefinition ("numberBean" ,
854860 new RootBeanDefinition (NumberBean .class , RootBeanDefinition .AUTOWIRE_CONSTRUCTOR , false ));
855861
@@ -922,7 +928,7 @@ public void testGenericMatchingWithFullTypeDifferentiation() {
922928 assertEquals (1 , resolved .size ());
923929 assertTrue (resolved .contains (bf .getBean ("store1" )));
924930
925- resolved = ( List ) doubleStoreProvider .orderedStream ().collect (Collectors .toList ());
931+ resolved = doubleStoreProvider .orderedStream ().collect (Collectors .toList ());
926932 assertEquals (1 , resolved .size ());
927933 assertTrue (resolved .contains (bf .getBean ("store1" )));
928934
@@ -937,11 +943,31 @@ public void testGenericMatchingWithFullTypeDifferentiation() {
937943 assertEquals (1 , resolved .size ());
938944 assertTrue (resolved .contains (bf .getBean ("store2" )));
939945
940- resolved = ( List ) floatStoreProvider .orderedStream ().collect (Collectors .toList ());
946+ resolved = floatStoreProvider .orderedStream ().collect (Collectors .toList ());
941947 assertEquals (1 , resolved .size ());
942948 assertTrue (resolved .contains (bf .getBean ("store2" )));
943949 }
944950
951+ @ Test
952+ public void testGenericMatchingWithUnresolvedOrderedStream () {
953+ DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
954+ bf .setDependencyComparator (AnnotationAwareOrderComparator .INSTANCE );
955+ bf .setAutowireCandidateResolver (new GenericTypeAwareAutowireCandidateResolver ());
956+
957+ RootBeanDefinition bd1 = new RootBeanDefinition (NumberStoreFactory .class );
958+ bd1 .setFactoryMethodName ("newDoubleStore" );
959+ bf .registerBeanDefinition ("store1" , bd1 );
960+ RootBeanDefinition bd2 = new RootBeanDefinition (NumberStoreFactory .class );
961+ bd2 .setFactoryMethodName ("newFloatStore" );
962+ bf .registerBeanDefinition ("store2" , bd2 );
963+
964+ ObjectProvider <NumberStore <?>> numberStoreProvider = bf .getBeanProvider (ResolvableType .forClass (NumberStore .class ));
965+ List <NumberStore <?>> resolved = numberStoreProvider .orderedStream ().collect (Collectors .toList ());
966+ assertEquals (2 , resolved .size ());
967+ assertSame (bf .getBean ("store2" ), resolved .get (0 ));
968+ assertSame (bf .getBean ("store1" ), resolved .get (1 ));
969+ }
970+
945971
946972 @ SuppressWarnings ("serial" )
947973 public static class NamedUrlList extends LinkedList <URL > {
@@ -1009,21 +1035,11 @@ public static class NumberStore<T extends Number> {
10091035 }
10101036
10111037
1012- public static class DoubleStore extends NumberStore <Double > implements Ordered {
1013-
1014- @ Override
1015- public int getOrder () {
1016- return 1 ;
1017- }
1038+ public static class DoubleStore extends NumberStore <Double > {
10181039 }
10191040
10201041
1021- public static class FloatStore extends NumberStore <Float > implements Ordered {
1022-
1023- @ Override
1024- public int getOrder () {
1025- return 0 ;
1026- }
1042+ public static class FloatStore extends NumberStore <Float > {
10271043 }
10281044
10291045
@@ -1047,4 +1063,18 @@ public NumberStore<Float> getFloatStore() {
10471063 }
10481064 }
10491065
1066+
1067+ public static class NumberStoreFactory {
1068+
1069+ @ Order (1 )
1070+ public static NumberStore <Double > newDoubleStore () {
1071+ return new DoubleStore ();
1072+ }
1073+
1074+ @ Order (0 )
1075+ public static NumberStore <Float > newFloatStore () {
1076+ return new FloatStore ();
1077+ }
1078+ }
1079+
10501080}
0 commit comments