Update OnBeanCondition to consider hierarchy for PARENTS search strategy

Closes gh-6762
This commit is contained in:
Andy Wilkinson 2016-08-31 13:26:25 +01:00
parent c8632f12f6
commit 63b8e82c10
3 changed files with 95 additions and 3 deletions

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -135,7 +135,7 @@ class OnBeanCondition extends SpringBootCondition implements ConfigurationCondit
return Collections.emptyList(); return Collections.emptyList();
} }
List<String> beanNames = new ArrayList<String>(); List<String> beanNames = new ArrayList<String>();
boolean considerHierarchy = beans.getStrategy() == SearchStrategy.ALL; boolean considerHierarchy = beans.getStrategy() != SearchStrategy.CURRENT;
for (String type : beans.getTypes()) { for (String type : beans.getTypes()) {
beanNames.addAll(getBeanNamesForType(beanFactory, type, beanNames.addAll(getBeanNamesForType(beanFactory, type,
context.getClassLoader(), considerHierarchy)); context.getClassLoader(), considerHierarchy));

View File

@ -250,6 +250,45 @@ public class ConditionalOnMissingBeanTests {
is(equalTo(1))); is(equalTo(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).size(), is(equalTo(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).size(), is(equalTo(2)));
}
@Configuration
protected static class OnBeanInParentsConfiguration {
@Bean
@ConditionalOnMissingBean(search = SearchStrategy.PARENTS)
public ExampleBean exampleBean2() {
return new ExampleBean("test");
}
}
@Configuration @Configuration
@ConditionalOnMissingBean(name = "foo") @ConditionalOnMissingBean(name = "foo")
protected static class OnBeanNameConfiguration { protected static class OnBeanNameConfiguration {

View File

@ -64,6 +64,46 @@ public class ConditionalOnSingleCandidateTests {
assertEquals("foo", this.context.getBean("baz")); assertEquals("foo", this.context.getBean("baz"));
} }
@Test
public void singleCandidateInParentsOneCandidateInCurrent() {
load();
AnnotationConfigApplicationContext child = new AnnotationConfigApplicationContext();
child.register(FooConfiguration.class,
OnBeanSingleCandidateInParentsConfiguration.class);
child.setParent(this.context);
child.refresh();
assertFalse(child.containsBean("baz"));
child.close();
}
@Test
public void singleCandidateInParentsOneCandidateInParent() {
load(FooConfiguration.class);
AnnotationConfigApplicationContext child = new AnnotationConfigApplicationContext();
child.register(OnBeanSingleCandidateInParentsConfiguration.class);
child.setParent(this.context);
child.refresh();
assertTrue(child.containsBean("baz"));
assertEquals("foo", child.getBean("baz"));
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();
assertTrue(child.containsBean("baz"));
assertEquals("foo", child.getBean("baz"));
child.close();
parent.close();
}
@Test @Test
public void singleCandidateMultipleCandidates() { public void singleCandidateMultipleCandidates() {
load(FooConfiguration.class, BarConfiguration.class, load(FooConfiguration.class, BarConfiguration.class,
@ -121,7 +161,9 @@ public class ConditionalOnSingleCandidateTests {
} }
private void load(Class<?>... classes) { private void load(Class<?>... classes) {
this.context.register(classes); if (classes.length > 0) {
this.context.register(classes);
}
this.context.refresh(); this.context.refresh();
} }
@ -136,6 +178,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 @Configuration
@ConditionalOnSingleCandidate(value = String.class, type = "java.lang.String") @ConditionalOnSingleCandidate(value = String.class, type = "java.lang.String")
protected static class OnBeanSingleCandidateTwoTypesConfiguration { protected static class OnBeanSingleCandidateTwoTypesConfiguration {