Merge branch '1.3.x'
This commit is contained in:
@@ -94,7 +94,8 @@ class OnBeanCondition extends SpringBootCondition implements ConfigurationCondit
|
||||
return ConditionOutcome.noMatch(
|
||||
"@ConditionalOnSingleCandidate " + spec + " found no beans");
|
||||
}
|
||||
else if (!hasSingleAutowireCandidate(context.getBeanFactory(), matching)) {
|
||||
else if (!hasSingleAutowireCandidate(context.getBeanFactory(), matching,
|
||||
spec.getStrategy() == SearchStrategy.ALL)) {
|
||||
return ConditionOutcome.noMatch("@ConditionalOnSingleCandidate " + spec
|
||||
+ " found no primary candidate amongst the" + " following "
|
||||
+ matching);
|
||||
@@ -222,16 +223,19 @@ class OnBeanCondition extends SpringBootCondition implements ConfigurationCondit
|
||||
}
|
||||
|
||||
private boolean hasSingleAutowireCandidate(
|
||||
ConfigurableListableBeanFactory beanFactory, List<String> beanNames) {
|
||||
ConfigurableListableBeanFactory beanFactory, List<String> beanNames,
|
||||
boolean considerHierarchy) {
|
||||
return (beanNames.size() == 1
|
||||
|| getPrimaryBeans(beanFactory, beanNames).size() == 1);
|
||||
|| getPrimaryBeans(beanFactory, beanNames, considerHierarchy)
|
||||
.size() == 1);
|
||||
}
|
||||
|
||||
private List<String> getPrimaryBeans(ConfigurableListableBeanFactory beanFactory,
|
||||
List<String> beanNames) {
|
||||
List<String> beanNames, boolean considerHierarchy) {
|
||||
List<String> primaryBeans = new ArrayList<String>();
|
||||
for (String beanName : beanNames) {
|
||||
BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName);
|
||||
BeanDefinition beanDefinition = findBeanDefinition(beanFactory, beanName,
|
||||
considerHierarchy);
|
||||
if (beanDefinition != null && beanDefinition.isPrimary()) {
|
||||
primaryBeans.add(beanName);
|
||||
}
|
||||
@@ -239,6 +243,20 @@ class OnBeanCondition extends SpringBootCondition implements ConfigurationCondit
|
||||
return primaryBeans;
|
||||
}
|
||||
|
||||
private BeanDefinition findBeanDefinition(ConfigurableListableBeanFactory beanFactory,
|
||||
String beanName, boolean considerHierarchy) {
|
||||
if (beanFactory.containsBeanDefinition(beanName)) {
|
||||
return beanFactory.getBeanDefinition(beanName);
|
||||
}
|
||||
if (considerHierarchy && beanFactory
|
||||
.getParentBeanFactory() instanceof ConfigurableListableBeanFactory) {
|
||||
return findBeanDefinition(((ConfigurableListableBeanFactory) beanFactory
|
||||
.getParentBeanFactory()), beanName, considerHierarchy);
|
||||
}
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
private static class BeanSearchSpec {
|
||||
|
||||
private final Class<?> annotationType;
|
||||
|
||||
@@ -33,6 +33,7 @@ import static org.hamcrest.CoreMatchers.isA;
|
||||
* Tests for {@link ConditionalOnSingleCandidate}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
public class ConditionalOnSingleCandidateTests {
|
||||
|
||||
@@ -101,6 +102,22 @@ public class ConditionalOnSingleCandidateTests {
|
||||
load(OnBeanSingleCandidateNoTypeConfiguration.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void singleCandidateMultipleCandidatesInContextHierarchy() {
|
||||
load(FooPrimaryConfiguration.class, BarConfiguration.class);
|
||||
AnnotationConfigApplicationContext child = new AnnotationConfigApplicationContext();
|
||||
child.setParent(this.context);
|
||||
child.register(OnBeanSingleCandidateConfiguration.class);
|
||||
try {
|
||||
child.refresh();
|
||||
assertThat(child.containsBean("baz")).isTrue();
|
||||
assertThat(child.getBean("baz")).isEqualTo("foo");
|
||||
}
|
||||
finally {
|
||||
child.close();
|
||||
}
|
||||
}
|
||||
|
||||
private void load(Class<?>... classes) {
|
||||
this.context.register(classes);
|
||||
this.context.refresh();
|
||||
|
||||
Reference in New Issue
Block a user