Skip to content

Commit 90f375e

Browse files
committed
Dont deduce type for OnBean conditions when annotations are specified
Update `OnBeanCondition` to consider the annotations attribute as well as the types and names when determining if the bean type can be deduced. Fixes gh-42484
1 parent fc2878a commit 90f375e

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

spring-boot-project/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
@@ -546,7 +546,7 @@ private static class Spec<A extends Annotation> {
546546
this.strategy = annotation.getValue("search", SearchStrategy.class).orElse(null);
547547
Set<String> types = extractTypes(attributes);
548548
BeanTypeDeductionException deductionException = null;
549-
if (types.isEmpty() && this.names.isEmpty()) {
549+
if (types.isEmpty() && this.names.isEmpty() && this.annotations.isEmpty()) {
550550
try {
551551
types = deducedBeanType(context, metadata);
552552
}
@@ -602,7 +602,7 @@ private Set<Class<?>> resolveWhenPossible(Set<String> classNames) {
602602
}
603603

604604
protected void validate(BeanTypeDeductionException ex) {
605-
if (!hasAtLeastOneElement(this.types, this.names, this.annotations)) {
605+
if (!hasAtLeastOneElement(getTypes(), getNames(), getAnnotations())) {
606606
String message = getAnnotationName() + " did not specify a bean using type, name or annotation";
607607
if (ex == null) {
608608
throw new IllegalStateException(message);

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,14 @@ void testAnnotationOnMissingBeanConditionWithEagerFactoryBean() {
137137
});
138138
}
139139

140+
@Test // gh-42484
141+
void testAnnotationOnMissingBeanConditionOnMethodWhenNoAnnotatedBeans() {
142+
// There are no beans with @TestAnnotation but there is an UnrelatedExampleBean
143+
this.contextRunner
144+
.withUserConfiguration(UnrelatedExampleBeanConfiguration.class, OnAnnotationMethodConfiguration.class)
145+
.run((context) -> assertThat(context).hasBean("conditional"));
146+
}
147+
140148
@Test
141149
void testOnMissingBeanConditionOutputShouldNotContainConditionalOnBeanClassInMessage() {
142150
this.contextRunner.withUserConfiguration(OnBeanNameConfiguration.class).run((context) -> {
@@ -594,6 +602,17 @@ String bar() {
594602

595603
}
596604

605+
@Configuration(proxyBeanMethods = false)
606+
static class OnAnnotationMethodConfiguration {
607+
608+
@Bean
609+
@ConditionalOnMissingBean(annotation = TestAnnotation.class)
610+
UnrelatedExampleBean conditional() {
611+
return new UnrelatedExampleBean("conditional");
612+
}
613+
614+
}
615+
597616
@Configuration(proxyBeanMethods = false)
598617
@ConditionalOnMissingBean(annotation = TestAnnotation.class)
599618
static class OnAnnotationWithFactoryBeanConfiguration {
@@ -668,6 +687,16 @@ ExampleBean exampleBean() {
668687

669688
}
670689

690+
@Configuration(proxyBeanMethods = false)
691+
static class UnrelatedExampleBeanConfiguration {
692+
693+
@Bean
694+
UnrelatedExampleBean unrelatedExampleBean() {
695+
return new UnrelatedExampleBean("test");
696+
}
697+
698+
}
699+
671700
@Configuration(proxyBeanMethods = false)
672701
static class ImpliedOnBeanMethod {
673702

@@ -851,6 +880,21 @@ static class OtherExampleBean extends ExampleBean {
851880

852881
}
853882

883+
static class UnrelatedExampleBean {
884+
885+
private final String value;
886+
887+
UnrelatedExampleBean(String value) {
888+
this.value = value;
889+
}
890+
891+
@Override
892+
public String toString() {
893+
return this.value;
894+
}
895+
896+
}
897+
854898
@Target(ElementType.TYPE)
855899
@Retention(RetentionPolicy.RUNTIME)
856900
@Documented

0 commit comments

Comments
 (0)