Skip to content

Commit 5281f5c

Browse files
committed
Merge branch '3.5.x'
The issue had already been fixed in 4.0 as part of the nullability work. As such, this commit doesn't fix anything. It does, however, merge forward a test and makes some stylistic changes to the code so that it's aligned with the same code in 3.5.x and 3.4.x
2 parents 3d23261 + 91cb68b commit 5281f5c

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -472,13 +472,19 @@ private void appendMessageForMatches(StringBuilder reason, Map<String, Collectio
472472
}
473473

474474
private List<String> getPrimaryBeans(Map<String, @Nullable BeanDefinition> beanDefinitions) {
475-
return getMatchingBeans(beanDefinitions,
476-
(beanDefinition) -> beanDefinition != null && beanDefinition.isPrimary());
475+
return getMatchingBeans(beanDefinitions, this::isPrimary);
476+
}
477+
478+
private boolean isPrimary(@Nullable BeanDefinition beanDefinition) {
479+
return (beanDefinition != null) && beanDefinition.isPrimary();
477480
}
478481

479482
private List<String> getNonFallbackBeans(Map<String, @Nullable BeanDefinition> beanDefinitions) {
480-
return getMatchingBeans(beanDefinitions,
481-
Predicate.not((beanDefinition) -> beanDefinition != null && beanDefinition.isFallback()));
483+
return getMatchingBeans(beanDefinitions, this::isNotFallback);
484+
}
485+
486+
private boolean isNotFallback(@Nullable BeanDefinition beanDefinition) {
487+
return (beanDefinition == null) || !beanDefinition.isFallback();
482488
}
483489

484490
private List<String> getMatchingBeans(Map<String, @Nullable BeanDefinition> beanDefinitions,

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,16 @@ void singleCandidateMultipleCandidatesOneDefaultCandidate() {
195195
});
196196
}
197197

198+
@Test
199+
void singleCandidateDoesNotMatchWhenMultipleRegisteredAsSingletonCandidates() {
200+
this.contextRunner.withInitializer((context) -> {
201+
context.getBeanFactory().registerSingleton("alpha", "alpha");
202+
context.getBeanFactory().registerSingleton("bravo", "bravo");
203+
})
204+
.withUserConfiguration(OnBeanSingleCandidateConfiguration.class)
205+
.run((context) -> assertThat(context).doesNotHaveBean("consumer"));
206+
}
207+
198208
@Configuration(proxyBeanMethods = false)
199209
@ConditionalOnSingleCandidate(String.class)
200210
static class OnBeanSingleCandidateConfiguration {

0 commit comments

Comments
 (0)