Skip to content

Commit 6cfbf39

Browse files
committed
Merge branch '5.3.x'
2 parents b0ee513 + 0d2bfc9 commit 6cfbf39

File tree

3 files changed

+111
-10
lines changed

3 files changed

+111
-10
lines changed

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

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2149,20 +2149,25 @@ public FactoryAwareOrderSourceProvider(Map<Object, String> instancesToBeanNames)
21492149
@Nullable
21502150
public Object getOrderSource(Object obj) {
21512151
String beanName = this.instancesToBeanNames.get(obj);
2152-
if (beanName == null || !containsBeanDefinition(beanName)) {
2152+
if (beanName == null) {
21532153
return null;
21542154
}
2155-
RootBeanDefinition beanDefinition = getMergedLocalBeanDefinition(beanName);
2156-
List<Object> sources = new ArrayList<>(2);
2157-
Method factoryMethod = beanDefinition.getResolvedFactoryMethod();
2158-
if (factoryMethod != null) {
2159-
sources.add(factoryMethod);
2155+
try {
2156+
RootBeanDefinition beanDefinition = (RootBeanDefinition) getMergedBeanDefinition(beanName);
2157+
List<Object> sources = new ArrayList<>(2);
2158+
Method factoryMethod = beanDefinition.getResolvedFactoryMethod();
2159+
if (factoryMethod != null) {
2160+
sources.add(factoryMethod);
2161+
}
2162+
Class<?> targetType = beanDefinition.getTargetType();
2163+
if (targetType != null && targetType != obj.getClass()) {
2164+
sources.add(targetType);
2165+
}
2166+
return sources.toArray();
21602167
}
2161-
Class<?> targetType = beanDefinition.getTargetType();
2162-
if (targetType != null && targetType != obj.getClass()) {
2163-
sources.add(targetType);
2168+
catch (NoSuchBeanDefinitionException ex) {
2169+
return null;
21642170
}
2165-
return sources.toArray();
21662171
}
21672172
}
21682173

spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2010,6 +2010,21 @@ void autowireBeanByTypePrimaryTakesPrecedenceOverPriority() {
20102010
assertThat(bean.getSpouse()).isEqualTo(lbf.getBean("spouse"));
20112011
}
20122012

2013+
@Test
2014+
void beanProviderWithParentBeanFactoryReuseOrder() {
2015+
DefaultListableBeanFactory parentBf = new DefaultListableBeanFactory();
2016+
parentBf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);
2017+
parentBf.registerBeanDefinition("regular", new RootBeanDefinition(TestBean.class));
2018+
parentBf.registerBeanDefinition("test", new RootBeanDefinition(HighPriorityTestBean.class));
2019+
lbf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);
2020+
lbf.setParentBeanFactory(parentBf);
2021+
lbf.registerBeanDefinition("low", new RootBeanDefinition(LowPriorityTestBean.class));
2022+
List<Class<?>> orderedTypes = lbf.getBeanProvider(TestBean.class).orderedStream()
2023+
.map(Object::getClass).collect(Collectors.toList());
2024+
assertThat(orderedTypes).containsExactly(
2025+
HighPriorityTestBean.class, LowPriorityTestBean.class, TestBean.class);
2026+
}
2027+
20132028
@Test
20142029
void autowireExistingBeanByName() {
20152030
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright 2002-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.context.annotation;
18+
19+
import java.util.List;
20+
import java.util.stream.Collectors;
21+
22+
import org.junit.jupiter.api.Test;
23+
24+
import org.springframework.core.annotation.Order;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
28+
/**
29+
* Tests for gh-29105.
30+
*
31+
* @author Stephane Nicoll
32+
*/
33+
public class Gh29105Tests {
34+
35+
@Test
36+
void beanProviderWithParentContextReuseOrder() {
37+
AnnotationConfigApplicationContext parent = new AnnotationConfigApplicationContext();
38+
parent.register(DefaultConfiguration.class);
39+
parent.register(CustomConfiguration.class);
40+
parent.refresh();
41+
42+
AnnotationConfigApplicationContext child = new AnnotationConfigApplicationContext();
43+
child.setParent(parent);
44+
child.register(DefaultConfiguration.class);
45+
child.refresh();
46+
47+
List<Class<?>> orderedTypes = child.getBeanProvider(MyService.class)
48+
.orderedStream().map(Object::getClass).collect(Collectors.toList());
49+
assertThat(orderedTypes).containsExactly(CustomService.class, DefaultService.class);
50+
}
51+
52+
53+
interface MyService {}
54+
55+
static class CustomService implements MyService {}
56+
57+
static class DefaultService implements MyService {}
58+
59+
60+
@Configuration
61+
static class CustomConfiguration {
62+
63+
@Bean
64+
@Order(-1)
65+
CustomService customService() {
66+
return new CustomService();
67+
}
68+
69+
}
70+
71+
@Configuration
72+
static class DefaultConfiguration {
73+
74+
@Bean
75+
@Order(0)
76+
DefaultService defaultService() {
77+
return new DefaultService();
78+
}
79+
80+
}
81+
}

0 commit comments

Comments
 (0)