Skip to content

Commit 7562761

Browse files
committed
Ordered streams consistently operate on resolved bean instances
Issue: SPR-17272
1 parent 3f5a7bb commit 7562761

File tree

3 files changed

+106
-58
lines changed

3 files changed

+106
-58
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1459,7 +1459,8 @@ private void addCandidateEntry(Map<String, Object> candidates, String candidateN
14591459
candidates.put(candidateName, beanInstance);
14601460
}
14611461
}
1462-
else if (containsSingleton(candidateName)) {
1462+
else if (containsSingleton(candidateName) || (descriptor instanceof StreamDependencyDescriptor &&
1463+
((StreamDependencyDescriptor) descriptor).isOrdered())) {
14631464
Object beanInstance = descriptor.resolveCandidate(candidateName, requiredType, this);
14641465
candidates.put(candidateName, (beanInstance instanceof NullBean ? null : beanInstance));
14651466
}

spring-beans/src/test/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessorTests.java

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@
7171
import org.springframework.tests.sample.beans.TestBean;
7272
import org.springframework.util.ReflectionUtils;
7373
import org.springframework.util.SerializationTestUtils;
74-
import org.springframework.util.comparator.Comparators;
7574

7675
import static org.junit.Assert.*;
7776

@@ -1290,12 +1289,12 @@ public void testObjectProviderInjectionWithTargetNotUnique() {
12901289
@Test
12911290
public void testObjectProviderInjectionWithTargetPrimary() {
12921291
bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ObjectProviderInjectionBean.class));
1293-
RootBeanDefinition tb1 = new RootBeanDefinition(OrderedTestBean.class);
1294-
tb1.getPropertyValues().add("order", 1);
1292+
RootBeanDefinition tb1 = new RootBeanDefinition(TestBeanFactory.class);
1293+
tb1.setFactoryMethodName("newTestBean1");
12951294
tb1.setPrimary(true);
12961295
bf.registerBeanDefinition("testBean1", tb1);
1297-
RootBeanDefinition tb2 = new RootBeanDefinition(OrderedTestBean.class);
1298-
tb2.getPropertyValues().add("order", 0);
1296+
RootBeanDefinition tb2 = new RootBeanDefinition(TestBeanFactory.class);
1297+
tb2.setFactoryMethodName("newTestBean2");
12991298
tb2.setLazyInit(true);
13001299
bf.registerBeanDefinition("testBean2", tb2);
13011300

@@ -1321,8 +1320,27 @@ public void testObjectProviderInjectionWithTargetPrimary() {
13211320
assertSame(bf.getBean("testBean2"), testBeans.get(1));
13221321
testBeans = bean.sortedTestBeans();
13231322
assertEquals(2, testBeans.size());
1323+
assertSame(bf.getBean("testBean2"), testBeans.get(0));
13241324
assertSame(bf.getBean("testBean1"), testBeans.get(1));
1325+
}
1326+
1327+
@Test
1328+
public void testObjectProviderInjectionWithUnresolvedOrderedStream() {
1329+
bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ObjectProviderInjectionBean.class));
1330+
RootBeanDefinition tb1 = new RootBeanDefinition(TestBeanFactory.class);
1331+
tb1.setFactoryMethodName("newTestBean1");
1332+
tb1.setPrimary(true);
1333+
bf.registerBeanDefinition("testBean1", tb1);
1334+
RootBeanDefinition tb2 = new RootBeanDefinition(TestBeanFactory.class);
1335+
tb2.setFactoryMethodName("newTestBean2");
1336+
tb2.setLazyInit(true);
1337+
bf.registerBeanDefinition("testBean2", tb2);
1338+
1339+
ObjectProviderInjectionBean bean = (ObjectProviderInjectionBean) bf.getBean("annotatedBean");
1340+
List<?> testBeans = bean.sortedTestBeans();
1341+
assertEquals(2, testBeans.size());
13251342
assertSame(bf.getBean("testBean2"), testBeans.get(0));
1343+
assertSame(bf.getBean("testBean1"), testBeans.get(1));
13261344
}
13271345

13281346
@Test
@@ -2981,21 +2999,6 @@ public boolean isSingleton() {
29812999
}
29823000

29833001

2984-
public static class OrderedTestBean extends TestBean implements Ordered {
2985-
2986-
private int order;
2987-
2988-
public void setOrder(int order) {
2989-
this.order = order;
2990-
}
2991-
2992-
@Override
2993-
public int getOrder() {
2994-
return this.order;
2995-
}
2996-
}
2997-
2998-
29993002
public static class OrderedNestedTestBean extends NestedTestBean implements Ordered {
30003003

30013004
private int order;
@@ -3861,4 +3864,18 @@ public static SelfInjectingFactoryBean create() {
38613864
}
38623865
}
38633866

3867+
3868+
public static class TestBeanFactory {
3869+
3870+
@Order(1)
3871+
public static TestBean newTestBean1() {
3872+
return new TestBean();
3873+
}
3874+
3875+
@Order(0)
3876+
public static TestBean newTestBean2() {
3877+
return new TestBean();
3878+
}
3879+
}
3880+
38643881
}

spring-beans/src/test/java/org/springframework/beans/factory/support/BeanFactoryGenericsTests.java

Lines changed: 67 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,10 @@
4343
import org.springframework.beans.factory.config.TypedStringValue;
4444
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
4545
import org.springframework.beans.propertyeditors.CustomNumberEditor;
46-
import org.springframework.core.Ordered;
4746
import org.springframework.core.OverridingClassLoader;
4847
import org.springframework.core.ResolvableType;
48+
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
49+
import org.springframework.core.annotation.Order;
4950
import org.springframework.core.io.ClassPathResource;
5051
import org.springframework.core.io.UrlResource;
5152
import 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

Comments
 (0)