43
43
import org .springframework .beans .factory .config .TypedStringValue ;
44
44
import org .springframework .beans .factory .xml .XmlBeanDefinitionReader ;
45
45
import org .springframework .beans .propertyeditors .CustomNumberEditor ;
46
- import org .springframework .core .Ordered ;
47
46
import org .springframework .core .OverridingClassLoader ;
48
47
import org .springframework .core .ResolvableType ;
48
+ import org .springframework .core .annotation .AnnotationAwareOrderComparator ;
49
+ import org .springframework .core .annotation .Order ;
49
50
import org .springframework .core .io .ClassPathResource ;
50
51
import org .springframework .core .io .UrlResource ;
51
52
import org .springframework .tests .Assume ;
@@ -83,7 +84,7 @@ public void testGenericSetProperty() {
83
84
}
84
85
85
86
@ Test
86
- public void testGenericListProperty () throws MalformedURLException {
87
+ public void testGenericListProperty () throws Exception {
87
88
DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
88
89
RootBeanDefinition rbd = new RootBeanDefinition (GenericBean .class );
89
90
@@ -100,7 +101,7 @@ public void testGenericListProperty() throws MalformedURLException {
100
101
}
101
102
102
103
@ Test
103
- public void testGenericListPropertyWithAutowiring () throws MalformedURLException {
104
+ public void testGenericListPropertyWithAutowiring () throws Exception {
104
105
DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
105
106
bf .registerSingleton ("resource1" , new UrlResource ("http://localhost:8080" ));
106
107
bf .registerSingleton ("resource2" , new UrlResource ("http://localhost:9090" ));
@@ -223,7 +224,7 @@ public void testGenericSetConstructorWithOptionalAutowiring() {
223
224
}
224
225
225
226
@ Test
226
- public void testGenericSetListConstructor () throws MalformedURLException {
227
+ public void testGenericSetListConstructor () throws Exception {
227
228
DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
228
229
RootBeanDefinition rbd = new RootBeanDefinition (GenericBean .class );
229
230
@@ -246,7 +247,7 @@ public void testGenericSetListConstructor() throws MalformedURLException {
246
247
}
247
248
248
249
@ Test
249
- public void testGenericSetListConstructorWithAutowiring () throws MalformedURLException {
250
+ public void testGenericSetListConstructorWithAutowiring () throws Exception {
250
251
DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
251
252
bf .registerSingleton ("integer1" , new Integer (4 ));
252
253
bf .registerSingleton ("integer2" , new Integer (5 ));
@@ -265,7 +266,7 @@ public void testGenericSetListConstructorWithAutowiring() throws MalformedURLExc
265
266
}
266
267
267
268
@ Test
268
- public void testGenericSetListConstructorWithOptionalAutowiring () throws MalformedURLException {
269
+ public void testGenericSetListConstructorWithOptionalAutowiring () throws Exception {
269
270
DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
270
271
bf .registerSingleton ("resource1" , new UrlResource ("http://localhost:8080" ));
271
272
bf .registerSingleton ("resource2" , new UrlResource ("http://localhost:9090" ));
@@ -280,7 +281,7 @@ public void testGenericSetListConstructorWithOptionalAutowiring() throws Malform
280
281
}
281
282
282
283
@ Test
283
- public void testGenericSetMapConstructor () throws MalformedURLException {
284
+ public void testGenericSetMapConstructor () {
284
285
DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
285
286
RootBeanDefinition rbd = new RootBeanDefinition (GenericBean .class );
286
287
@@ -303,7 +304,7 @@ public void testGenericSetMapConstructor() throws MalformedURLException {
303
304
}
304
305
305
306
@ Test
306
- public void testGenericMapResourceConstructor () throws MalformedURLException {
307
+ public void testGenericMapResourceConstructor () throws Exception {
307
308
DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
308
309
RootBeanDefinition rbd = new RootBeanDefinition (GenericBean .class );
309
310
@@ -322,7 +323,7 @@ public void testGenericMapResourceConstructor() throws MalformedURLException {
322
323
}
323
324
324
325
@ Test
325
- public void testGenericMapMapConstructor () throws MalformedURLException {
326
+ public void testGenericMapMapConstructor () {
326
327
DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
327
328
RootBeanDefinition rbd = new RootBeanDefinition (GenericBean .class );
328
329
@@ -348,7 +349,7 @@ public void testGenericMapMapConstructor() throws MalformedURLException {
348
349
}
349
350
350
351
@ Test
351
- public void testGenericMapMapConstructorWithSameRefAndConversion () throws MalformedURLException {
352
+ public void testGenericMapMapConstructorWithSameRefAndConversion () {
352
353
DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
353
354
RootBeanDefinition rbd = new RootBeanDefinition (GenericBean .class );
354
355
@@ -371,7 +372,7 @@ public void testGenericMapMapConstructorWithSameRefAndConversion() throws Malfor
371
372
}
372
373
373
374
@ Test
374
- public void testGenericMapMapConstructorWithSameRefAndNoConversion () throws MalformedURLException {
375
+ public void testGenericMapMapConstructorWithSameRefAndNoConversion () {
375
376
DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
376
377
RootBeanDefinition rbd = new RootBeanDefinition (GenericBean .class );
377
378
@@ -391,7 +392,7 @@ public void testGenericMapMapConstructorWithSameRefAndNoConversion() throws Malf
391
392
}
392
393
393
394
@ Test
394
- public void testGenericMapWithKeyTypeConstructor () throws MalformedURLException {
395
+ public void testGenericMapWithKeyTypeConstructor () {
395
396
DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
396
397
RootBeanDefinition rbd = new RootBeanDefinition (GenericBean .class );
397
398
@@ -408,7 +409,7 @@ public void testGenericMapWithKeyTypeConstructor() throws MalformedURLException
408
409
}
409
410
410
411
@ Test
411
- public void testGenericMapWithCollectionValueConstructor () throws MalformedURLException {
412
+ public void testGenericMapWithCollectionValueConstructor () {
412
413
DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
413
414
bf .addPropertyEditorRegistrar (new PropertyEditorRegistrar () {
414
415
@ Override
@@ -455,7 +456,7 @@ public void testGenericSetFactoryMethod() {
455
456
}
456
457
457
458
@ Test
458
- public void testGenericSetListFactoryMethod () throws MalformedURLException {
459
+ public void testGenericSetListFactoryMethod () throws Exception {
459
460
DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
460
461
RootBeanDefinition rbd = new RootBeanDefinition (GenericBean .class );
461
462
rbd .setFactoryMethodName ("createInstance" );
@@ -479,7 +480,7 @@ public void testGenericSetListFactoryMethod() throws MalformedURLException {
479
480
}
480
481
481
482
@ Test
482
- public void testGenericSetMapFactoryMethod () throws MalformedURLException {
483
+ public void testGenericSetMapFactoryMethod () {
483
484
DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
484
485
RootBeanDefinition rbd = new RootBeanDefinition (GenericBean .class );
485
486
rbd .setFactoryMethodName ("createInstance" );
@@ -503,7 +504,7 @@ public void testGenericSetMapFactoryMethod() throws MalformedURLException {
503
504
}
504
505
505
506
@ Test
506
- public void testGenericMapResourceFactoryMethod () throws MalformedURLException {
507
+ public void testGenericMapResourceFactoryMethod () throws Exception {
507
508
DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
508
509
RootBeanDefinition rbd = new RootBeanDefinition (GenericBean .class );
509
510
rbd .setFactoryMethodName ("createInstance" );
@@ -523,7 +524,7 @@ public void testGenericMapResourceFactoryMethod() throws MalformedURLException {
523
524
}
524
525
525
526
@ Test
526
- public void testGenericMapMapFactoryMethod () throws MalformedURLException {
527
+ public void testGenericMapMapFactoryMethod () {
527
528
DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
528
529
RootBeanDefinition rbd = new RootBeanDefinition (GenericBean .class );
529
530
rbd .setFactoryMethodName ("createInstance" );
@@ -547,7 +548,7 @@ public void testGenericMapMapFactoryMethod() throws MalformedURLException {
547
548
}
548
549
549
550
@ Test
550
- public void testGenericMapWithKeyTypeFactoryMethod () throws MalformedURLException {
551
+ public void testGenericMapWithKeyTypeFactoryMethod () {
551
552
DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
552
553
RootBeanDefinition rbd = new RootBeanDefinition (GenericBean .class );
553
554
rbd .setFactoryMethodName ("createInstance" );
@@ -565,7 +566,7 @@ public void testGenericMapWithKeyTypeFactoryMethod() throws MalformedURLExceptio
565
566
}
566
567
567
568
@ Test
568
- public void testGenericMapWithCollectionValueFactoryMethod () throws MalformedURLException {
569
+ public void testGenericMapWithCollectionValueFactoryMethod () {
569
570
DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
570
571
bf .addPropertyEditorRegistrar (new PropertyEditorRegistrar () {
571
572
@ Override
@@ -625,7 +626,7 @@ public void testGenericMapBean() throws Exception {
625
626
}
626
627
627
628
@ Test
628
- public void testGenericallyTypedIntegerBean () throws Exception {
629
+ public void testGenericallyTypedIntegerBean () {
629
630
DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
630
631
new XmlBeanDefinitionReader (bf ).loadBeanDefinitions (
631
632
new ClassPathResource ("genericBeanTests.xml" , getClass ()));
@@ -636,7 +637,7 @@ public void testGenericallyTypedIntegerBean() throws Exception {
636
637
}
637
638
638
639
@ Test
639
- public void testGenericallyTypedSetOfIntegerBean () throws Exception {
640
+ public void testGenericallyTypedSetOfIntegerBean () {
640
641
DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
641
642
new XmlBeanDefinitionReader (bf ).loadBeanDefinitions (
642
643
new ClassPathResource ("genericBeanTests.xml" , getClass ()));
@@ -846,10 +847,15 @@ public void testGenericMatchingWithBeanNameDifferentiation() {
846
847
@ Test
847
848
public void testGenericMatchingWithFullTypeDifferentiation () {
848
849
DefaultListableBeanFactory bf = new DefaultListableBeanFactory ();
850
+ bf .setDependencyComparator (AnnotationAwareOrderComparator .INSTANCE );
849
851
bf .setAutowireCandidateResolver (new GenericTypeAwareAutowireCandidateResolver ());
850
852
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 );
853
859
bf .registerBeanDefinition ("numberBean" ,
854
860
new RootBeanDefinition (NumberBean .class , RootBeanDefinition .AUTOWIRE_CONSTRUCTOR , false ));
855
861
@@ -922,7 +928,7 @@ public void testGenericMatchingWithFullTypeDifferentiation() {
922
928
assertEquals (1 , resolved .size ());
923
929
assertTrue (resolved .contains (bf .getBean ("store1" )));
924
930
925
- resolved = ( List ) doubleStoreProvider .orderedStream ().collect (Collectors .toList ());
931
+ resolved = doubleStoreProvider .orderedStream ().collect (Collectors .toList ());
926
932
assertEquals (1 , resolved .size ());
927
933
assertTrue (resolved .contains (bf .getBean ("store1" )));
928
934
@@ -937,11 +943,31 @@ public void testGenericMatchingWithFullTypeDifferentiation() {
937
943
assertEquals (1 , resolved .size ());
938
944
assertTrue (resolved .contains (bf .getBean ("store2" )));
939
945
940
- resolved = ( List ) floatStoreProvider .orderedStream ().collect (Collectors .toList ());
946
+ resolved = floatStoreProvider .orderedStream ().collect (Collectors .toList ());
941
947
assertEquals (1 , resolved .size ());
942
948
assertTrue (resolved .contains (bf .getBean ("store2" )));
943
949
}
944
950
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
+
945
971
946
972
@ SuppressWarnings ("serial" )
947
973
public static class NamedUrlList extends LinkedList <URL > {
@@ -1009,21 +1035,11 @@ public static class NumberStore<T extends Number> {
1009
1035
}
1010
1036
1011
1037
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 > {
1018
1039
}
1019
1040
1020
1041
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 > {
1027
1043
}
1028
1044
1029
1045
@@ -1047,4 +1063,18 @@ public NumberStore<Float> getFloatStore() {
1047
1063
}
1048
1064
}
1049
1065
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
+
1050
1080
}
0 commit comments