Skip to content

Commit 63b8e82

Browse files
committed
Update OnBeanCondition to consider hierarchy for PARENTS search strategy
Closes gh-6762
1 parent c8632f1 commit 63b8e82

File tree

3 files changed

+95
-3
lines changed

3 files changed

+95
-3
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/OnBeanCondition.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2015 the original author or authors.
2+
* Copyright 2012-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -135,7 +135,7 @@ private List<String> getMatchingBeans(ConditionContext context,
135135
return Collections.emptyList();
136136
}
137137
List<String> beanNames = new ArrayList<String>();
138-
boolean considerHierarchy = beans.getStrategy() == SearchStrategy.ALL;
138+
boolean considerHierarchy = beans.getStrategy() != SearchStrategy.CURRENT;
139139
for (String type : beans.getTypes()) {
140140
beanNames.addAll(getBeanNamesForType(beanFactory, type,
141141
context.getClassLoader(), considerHierarchy));

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionalOnMissingBeanTests.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,45 @@ public void testOnMissingBeanConditionWithIgnoredSubclassByName() {
250250
is(equalTo(1)));
251251
}
252252

253+
@Test
254+
public void grandparentIsConsideredWhenUsingParentsStrategy() {
255+
this.context.register(ExampleBeanConfiguration.class);
256+
this.context.refresh();
257+
AnnotationConfigApplicationContext parent = new AnnotationConfigApplicationContext();
258+
parent.setParent(this.context);
259+
parent.refresh();
260+
AnnotationConfigApplicationContext child = new AnnotationConfigApplicationContext();
261+
child.setParent(parent);
262+
child.register(ExampleBeanConfiguration.class,
263+
OnBeanInParentsConfiguration.class);
264+
child.refresh();
265+
assertThat(child.getBeansOfType(ExampleBean.class).size(), is(equalTo(1)));
266+
child.close();
267+
parent.close();
268+
}
269+
270+
@Test
271+
public void currentContextIsIgnoredWhenUsingParentsStrategy() {
272+
this.context.refresh();
273+
AnnotationConfigApplicationContext child = new AnnotationConfigApplicationContext();
274+
child.register(ExampleBeanConfiguration.class,
275+
OnBeanInParentsConfiguration.class);
276+
child.setParent(this.context);
277+
child.refresh();
278+
assertThat(child.getBeansOfType(ExampleBean.class).size(), is(equalTo(2)));
279+
}
280+
281+
@Configuration
282+
protected static class OnBeanInParentsConfiguration {
283+
284+
@Bean
285+
@ConditionalOnMissingBean(search = SearchStrategy.PARENTS)
286+
public ExampleBean exampleBean2() {
287+
return new ExampleBean("test");
288+
}
289+
290+
}
291+
253292
@Configuration
254293
@ConditionalOnMissingBean(name = "foo")
255294
protected static class OnBeanNameConfiguration {

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionalOnSingleCandidateTests.java

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,46 @@ public void singleCandidateOneCandidate() {
6464
assertEquals("foo", this.context.getBean("baz"));
6565
}
6666

67+
@Test
68+
public void singleCandidateInParentsOneCandidateInCurrent() {
69+
load();
70+
AnnotationConfigApplicationContext child = new AnnotationConfigApplicationContext();
71+
child.register(FooConfiguration.class,
72+
OnBeanSingleCandidateInParentsConfiguration.class);
73+
child.setParent(this.context);
74+
child.refresh();
75+
assertFalse(child.containsBean("baz"));
76+
child.close();
77+
}
78+
79+
@Test
80+
public void singleCandidateInParentsOneCandidateInParent() {
81+
load(FooConfiguration.class);
82+
AnnotationConfigApplicationContext child = new AnnotationConfigApplicationContext();
83+
child.register(OnBeanSingleCandidateInParentsConfiguration.class);
84+
child.setParent(this.context);
85+
child.refresh();
86+
assertTrue(child.containsBean("baz"));
87+
assertEquals("foo", child.getBean("baz"));
88+
child.close();
89+
}
90+
91+
@Test
92+
public void singleCandidateInParentsOneCandidateInGrandparent() {
93+
load(FooConfiguration.class);
94+
AnnotationConfigApplicationContext parent = new AnnotationConfigApplicationContext();
95+
parent.setParent(this.context);
96+
parent.refresh();
97+
AnnotationConfigApplicationContext child = new AnnotationConfigApplicationContext();
98+
child.register(OnBeanSingleCandidateInParentsConfiguration.class);
99+
child.setParent(parent);
100+
child.refresh();
101+
assertTrue(child.containsBean("baz"));
102+
assertEquals("foo", child.getBean("baz"));
103+
child.close();
104+
parent.close();
105+
}
106+
67107
@Test
68108
public void singleCandidateMultipleCandidates() {
69109
load(FooConfiguration.class, BarConfiguration.class,
@@ -121,7 +161,9 @@ public void singleCandidateMultipleCandidatesInContextHierarchy() {
121161
}
122162

123163
private void load(Class<?>... classes) {
124-
this.context.register(classes);
164+
if (classes.length > 0) {
165+
this.context.register(classes);
166+
}
125167
this.context.refresh();
126168
}
127169

@@ -136,6 +178,17 @@ public String baz(String s) {
136178

137179
}
138180

181+
@Configuration
182+
@ConditionalOnSingleCandidate(value = String.class, search = SearchStrategy.PARENTS)
183+
protected static class OnBeanSingleCandidateInParentsConfiguration {
184+
185+
@Bean
186+
public String baz(String s) {
187+
return s;
188+
}
189+
190+
}
191+
139192
@Configuration
140193
@ConditionalOnSingleCandidate(value = String.class, type = "java.lang.String")
141194
protected static class OnBeanSingleCandidateTwoTypesConfiguration {

0 commit comments

Comments
 (0)