Merge branch '1.3.x'

pull/6759/merge
Andy Wilkinson 8 years ago
commit b488a3d9a3

@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -132,7 +132,7 @@ class OnBeanCondition extends SpringBootCondition implements ConfigurationCondit
return Collections.emptyList();
}
List<String> beanNames = new ArrayList<String>();
boolean considerHierarchy = beans.getStrategy() == SearchStrategy.ALL;
boolean considerHierarchy = beans.getStrategy() != SearchStrategy.CURRENT;
for (String type : beans.getTypes()) {
beanNames.addAll(getBeanNamesForType(beanFactory, type,
context.getClassLoader(), considerHierarchy));

@ -257,6 +257,45 @@ public class ConditionalOnMissingBeanTests {
assertThat(this.context.getBeansOfType(CustomExampleBean.class)).hasSize(1);
}
@Test
public void grandparentIsConsideredWhenUsingParentsStrategy() {
this.context.register(ExampleBeanConfiguration.class);
this.context.refresh();
AnnotationConfigApplicationContext parent = new AnnotationConfigApplicationContext();
parent.setParent(this.context);
parent.refresh();
AnnotationConfigApplicationContext child = new AnnotationConfigApplicationContext();
child.setParent(parent);
child.register(ExampleBeanConfiguration.class,
OnBeanInParentsConfiguration.class);
child.refresh();
assertThat(child.getBeansOfType(ExampleBean.class)).hasSize(1);
child.close();
parent.close();
}
@Test
public void currentContextIsIgnoredWhenUsingParentsStrategy() {
this.context.refresh();
AnnotationConfigApplicationContext child = new AnnotationConfigApplicationContext();
child.register(ExampleBeanConfiguration.class,
OnBeanInParentsConfiguration.class);
child.setParent(this.context);
child.refresh();
assertThat(child.getBeansOfType(ExampleBean.class)).hasSize(1);
}
@Configuration
protected static class OnBeanInParentsConfiguration {
@Bean
@ConditionalOnMissingBean(search = SearchStrategy.PARENTS)
public ExampleBean exampleBean2() {
return new ExampleBean("test");
}
}
@Configuration
@ConditionalOnMissingBean(name = "foo")
protected static class OnBeanNameConfiguration {

@ -62,6 +62,46 @@ public class ConditionalOnSingleCandidateTests {
assertThat(this.context.getBean("baz")).isEqualTo("foo");
}
@Test
public void singleCandidateInParentsOneCandidateInCurrent() {
load();
AnnotationConfigApplicationContext child = new AnnotationConfigApplicationContext();
child.register(FooConfiguration.class,
OnBeanSingleCandidateInParentsConfiguration.class);
child.setParent(this.context);
child.refresh();
assertThat(child.containsBean("baz")).isFalse();
child.close();
}
@Test
public void singleCandidateInParentsOneCandidateInParent() {
load(FooConfiguration.class);
AnnotationConfigApplicationContext child = new AnnotationConfigApplicationContext();
child.register(OnBeanSingleCandidateInParentsConfiguration.class);
child.setParent(this.context);
child.refresh();
assertThat(child.containsBean("baz")).isTrue();
assertThat(child.getBean("baz")).isEqualTo("foo");
child.close();
}
@Test
public void singleCandidateInParentsOneCandidateInGrandparent() {
load(FooConfiguration.class);
AnnotationConfigApplicationContext parent = new AnnotationConfigApplicationContext();
parent.setParent(this.context);
parent.refresh();
AnnotationConfigApplicationContext child = new AnnotationConfigApplicationContext();
child.register(OnBeanSingleCandidateInParentsConfiguration.class);
child.setParent(parent);
child.refresh();
assertThat(child.containsBean("baz")).isTrue();
assertThat(child.getBean("baz")).isEqualTo("foo");
child.close();
parent.close();
}
@Test
public void singleCandidateMultipleCandidates() {
load(FooConfiguration.class, BarConfiguration.class,
@ -119,7 +159,9 @@ public class ConditionalOnSingleCandidateTests {
}
private void load(Class<?>... classes) {
this.context.register(classes);
if (classes.length > 0) {
this.context.register(classes);
}
this.context.refresh();
}
@ -134,6 +176,17 @@ public class ConditionalOnSingleCandidateTests {
}
@Configuration
@ConditionalOnSingleCandidate(value = String.class, search = SearchStrategy.PARENTS)
protected static class OnBeanSingleCandidateInParentsConfiguration {
@Bean
public String baz(String s) {
return s;
}
}
@Configuration
@ConditionalOnSingleCandidate(value = String.class, type = "java.lang.String")
protected static class OnBeanSingleCandidateTwoTypesConfiguration {

Loading…
Cancel
Save