Skip to content

Commit 547fc30

Browse files
Jorge Cordobasnicoll
authored andcommitted
Fix condition source in OnBeanCondition
See gh-19948
1 parent d485708 commit 547fc30

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
* @author Jakub Kubrynski
6868
* @author Stephane Nicoll
6969
* @author Andy Wilkinson
70+
* @author Jorge Cordoba
7071
* @see ConditionalOnBean
7172
* @see ConditionalOnMissingBean
7273
* @see ConditionalOnSingleCandidate
@@ -387,7 +388,7 @@ private static class Spec<A extends Annotation> {
387388

388389
private final ClassLoader classLoader;
389390

390-
private final Class<?> annotationType;
391+
private final Class<? extends Annotation> annotationType;
391392

392393
private final Set<String> names;
393394

@@ -581,11 +582,11 @@ Set<Class<?>> getParameterizedContainers() {
581582
}
582583

583584
ConditionMessage.Builder message() {
584-
return ConditionMessage.forCondition(ConditionalOnBean.class, this);
585+
return ConditionMessage.forCondition(this.annotationType, this);
585586
}
586587

587588
ConditionMessage.Builder message(ConditionMessage message) {
588-
return message.andCondition(ConditionalOnBean.class, this);
589+
return message.andCondition(this.annotationType, this);
589590
}
590591

591592
@Override

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.lang.annotation.Retention;
2222
import java.lang.annotation.RetentionPolicy;
2323
import java.lang.annotation.Target;
24+
import java.util.Collection;
2425
import java.util.Date;
2526
import java.util.function.Consumer;
2627

@@ -57,6 +58,7 @@
5758
* @author Phillip Webb
5859
* @author Jakub Kubrynski
5960
* @author Andy Wilkinson
61+
* @author Jorge Cordoba
6062
*/
6163
@SuppressWarnings("resource")
6264
public class ConditionalOnMissingBeanTests {
@@ -135,6 +137,16 @@ void testAnnotationOnMissingBeanConditionWithEagerFactoryBean() {
135137
assertThat(context.getBean("foo")).isEqualTo("foo");
136138
});
137139
}
140+
@Test
141+
void testOnMissingBeanConditionOutputShouldNotContainConditionalOnBeanClassInMessage() {
142+
this.contextRunner.withUserConfiguration(ConditionalOnMissingBeanTests.OnBeanNameConfiguration.class).run((context) -> {
143+
Collection<ConditionEvaluationReport.ConditionAndOutcomes> conditionAndOutcomes = ConditionEvaluationReport
144+
.get(context.getSourceApplicationContext().getBeanFactory()).getConditionAndOutcomesBySource()
145+
.values();
146+
String message = conditionAndOutcomes.iterator().next().iterator().next().getOutcome().getMessage();
147+
assertThat(message).doesNotContain("@ConditionalOnBean (names: foo; SearchStrategy: all) did not find any beans");
148+
});
149+
}
138150

139151
@Test
140152
void testOnMissingBeanConditionWithFactoryBean() {

0 commit comments

Comments
 (0)