Introduce SearchStrategy.ANCESTORS as a replacement for .PARENTS
Closes gh-6763
This commit is contained in:
parent
5d876125de
commit
2be554456e
|
|
@ -151,7 +151,7 @@ public class EndpointWebMvcChildContextConfiguration {
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@ConditionalOnClass({ EnableWebSecurity.class, Filter.class })
|
@ConditionalOnClass({ EnableWebSecurity.class, Filter.class })
|
||||||
@ConditionalOnBean(name = "springSecurityFilterChain", search = SearchStrategy.PARENTS)
|
@ConditionalOnBean(name = "springSecurityFilterChain", search = SearchStrategy.ANCESTORS)
|
||||||
public static class EndpointWebMvcChildContextSecurityConfiguration {
|
public static class EndpointWebMvcChildContextSecurityConfiguration {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
|
|
||||||
|
|
@ -123,10 +123,12 @@ class OnBeanCondition extends SpringBootCondition implements ConfigurationCondit
|
||||||
return ConditionOutcome.match(matchMessage);
|
return ConditionOutcome.match(matchMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("deprecation")
|
||||||
private List<String> getMatchingBeans(ConditionContext context,
|
private List<String> getMatchingBeans(ConditionContext context,
|
||||||
BeanSearchSpec beans) {
|
BeanSearchSpec beans) {
|
||||||
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
|
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
|
||||||
if (beans.getStrategy() == SearchStrategy.PARENTS) {
|
if (beans.getStrategy() == SearchStrategy.PARENTS
|
||||||
|
|| beans.getStrategy() == SearchStrategy.ANCESTORS) {
|
||||||
BeanFactory parent = beanFactory.getParentBeanFactory();
|
BeanFactory parent = beanFactory.getParentBeanFactory();
|
||||||
Assert.isInstanceOf(ConfigurableListableBeanFactory.class, parent,
|
Assert.isInstanceOf(ConfigurableListableBeanFactory.class, parent,
|
||||||
"Unable to use SearchStrategy.PARENTS");
|
"Unable to use SearchStrategy.PARENTS");
|
||||||
|
|
|
||||||
|
|
@ -30,8 +30,15 @@ public enum SearchStrategy {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Search all parents and ancestors, but not the current context.
|
* Search all parents and ancestors, but not the current context.
|
||||||
|
*
|
||||||
|
* @deprecated since 1.5 in favor of {@link SearchStrategy#ANCESTORS}
|
||||||
*/
|
*/
|
||||||
PARENTS,
|
@Deprecated PARENTS,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Search all ancestors, but not the current context.
|
||||||
|
*/
|
||||||
|
ANCESTORS,
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Search the entire hierarchy.
|
* Search the entire hierarchy.
|
||||||
|
|
|
||||||
|
|
@ -274,6 +274,23 @@ public class ConditionalOnMissingBeanTests {
|
||||||
parent.close();
|
parent.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void grandparentIsConsideredWhenUsingAncestorsStrategy() {
|
||||||
|
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,
|
||||||
|
OnBeanInAncestorsConfiguration.class);
|
||||||
|
child.refresh();
|
||||||
|
assertThat(child.getBeansOfType(ExampleBean.class)).hasSize(1);
|
||||||
|
child.close();
|
||||||
|
parent.close();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void currentContextIsIgnoredWhenUsingParentsStrategy() {
|
public void currentContextIsIgnoredWhenUsingParentsStrategy() {
|
||||||
this.context.refresh();
|
this.context.refresh();
|
||||||
|
|
@ -285,9 +302,21 @@ public class ConditionalOnMissingBeanTests {
|
||||||
assertThat(child.getBeansOfType(ExampleBean.class)).hasSize(2);
|
assertThat(child.getBeansOfType(ExampleBean.class)).hasSize(2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void currentContextIsIgnoredWhenUsingAncestorsStrategy() {
|
||||||
|
this.context.refresh();
|
||||||
|
AnnotationConfigApplicationContext child = new AnnotationConfigApplicationContext();
|
||||||
|
child.register(ExampleBeanConfiguration.class,
|
||||||
|
OnBeanInAncestorsConfiguration.class);
|
||||||
|
child.setParent(this.context);
|
||||||
|
child.refresh();
|
||||||
|
assertThat(child.getBeansOfType(ExampleBean.class)).hasSize(2);
|
||||||
|
}
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
protected static class OnBeanInParentsConfiguration {
|
protected static class OnBeanInParentsConfiguration {
|
||||||
|
|
||||||
|
@SuppressWarnings("deprecation")
|
||||||
@Bean
|
@Bean
|
||||||
@ConditionalOnMissingBean(search = SearchStrategy.PARENTS)
|
@ConditionalOnMissingBean(search = SearchStrategy.PARENTS)
|
||||||
public ExampleBean exampleBean2() {
|
public ExampleBean exampleBean2() {
|
||||||
|
|
@ -296,6 +325,17 @@ public class ConditionalOnMissingBeanTests {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
protected static class OnBeanInAncestorsConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@ConditionalOnMissingBean(search = SearchStrategy.ANCESTORS)
|
||||||
|
public ExampleBean exampleBean2() {
|
||||||
|
return new ExampleBean("test");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@ConditionalOnMissingBean(name = "foo")
|
@ConditionalOnMissingBean(name = "foo")
|
||||||
protected static class OnBeanNameConfiguration {
|
protected static class OnBeanNameConfiguration {
|
||||||
|
|
|
||||||
|
|
@ -102,6 +102,46 @@ public class ConditionalOnSingleCandidateTests {
|
||||||
parent.close();
|
parent.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void singleCandidateInAncestorsOneCandidateInCurrent() {
|
||||||
|
load();
|
||||||
|
AnnotationConfigApplicationContext child = new AnnotationConfigApplicationContext();
|
||||||
|
child.register(FooConfiguration.class,
|
||||||
|
OnBeanSingleCandidateInAncestorsConfiguration.class);
|
||||||
|
child.setParent(this.context);
|
||||||
|
child.refresh();
|
||||||
|
assertThat(child.containsBean("baz")).isFalse();
|
||||||
|
child.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void singleCandidateInAncestorsOneCandidateInParent() {
|
||||||
|
load(FooConfiguration.class);
|
||||||
|
AnnotationConfigApplicationContext child = new AnnotationConfigApplicationContext();
|
||||||
|
child.register(OnBeanSingleCandidateInAncestorsConfiguration.class);
|
||||||
|
child.setParent(this.context);
|
||||||
|
child.refresh();
|
||||||
|
assertThat(child.containsBean("baz")).isTrue();
|
||||||
|
assertThat(child.getBean("baz")).isEqualTo("foo");
|
||||||
|
child.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void singleCandidateInAncestorsOneCandidateInGrandparent() {
|
||||||
|
load(FooConfiguration.class);
|
||||||
|
AnnotationConfigApplicationContext parent = new AnnotationConfigApplicationContext();
|
||||||
|
parent.setParent(this.context);
|
||||||
|
parent.refresh();
|
||||||
|
AnnotationConfigApplicationContext child = new AnnotationConfigApplicationContext();
|
||||||
|
child.register(OnBeanSingleCandidateInAncestorsConfiguration.class);
|
||||||
|
child.setParent(parent);
|
||||||
|
child.refresh();
|
||||||
|
assertThat(child.containsBean("baz")).isTrue();
|
||||||
|
assertThat(child.getBean("baz")).isEqualTo("foo");
|
||||||
|
child.close();
|
||||||
|
parent.close();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void singleCandidateMultipleCandidates() {
|
public void singleCandidateMultipleCandidates() {
|
||||||
load(FooConfiguration.class, BarConfiguration.class,
|
load(FooConfiguration.class, BarConfiguration.class,
|
||||||
|
|
@ -176,6 +216,7 @@ public class ConditionalOnSingleCandidateTests {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("deprecation")
|
||||||
@Configuration
|
@Configuration
|
||||||
@ConditionalOnSingleCandidate(value = String.class, search = SearchStrategy.PARENTS)
|
@ConditionalOnSingleCandidate(value = String.class, search = SearchStrategy.PARENTS)
|
||||||
protected static class OnBeanSingleCandidateInParentsConfiguration {
|
protected static class OnBeanSingleCandidateInParentsConfiguration {
|
||||||
|
|
@ -187,6 +228,17 @@ public class ConditionalOnSingleCandidateTests {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@ConditionalOnSingleCandidate(value = String.class, search = SearchStrategy.ANCESTORS)
|
||||||
|
protected static class OnBeanSingleCandidateInAncestorsConfiguration {
|
||||||
|
|
||||||
|
@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 {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue