Skip to content

Commit 65ef03a

Browse files
committed
Remove nulls from description for bean defs with no res description
Previously, if a bean definition had no resource description, the failure analysis description would state that the been had been defined in null which was of no use to the user. This commit updates the failure analysis to omit information about where the bean was definied when the definition has no resource description. Fixes gh-18721
1 parent 8d4aeb1 commit 65ef03a

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/diagnostics/analyzer/BeanDefinitionOverrideFailureAnalyzer.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,15 @@ protected FailureAnalysis analyze(Throwable rootFailure, BeanDefinitionOverrideE
4242
private String getDescription(BeanDefinitionOverrideException ex) {
4343
StringWriter description = new StringWriter();
4444
PrintWriter printer = new PrintWriter(description);
45-
printer.printf(
46-
"The bean '%s', defined in %s, could not be registered. A bean with that "
47-
+ "name has already been defined in %s and overriding is disabled.",
48-
ex.getBeanName(), ex.getBeanDefinition().getResourceDescription(),
49-
ex.getExistingDefinition().getResourceDescription());
45+
printer.printf("The bean '%s'", ex.getBeanName());
46+
if (ex.getBeanDefinition().getResourceDescription() != null) {
47+
printer.printf(", defined in %s,", ex.getBeanDefinition().getResourceDescription());
48+
}
49+
printer.printf(" could not be registered. A bean with that name has already been defined ");
50+
if (ex.getExistingDefinition().getResourceDescription() != null) {
51+
printer.printf("in %s ", ex.getExistingDefinition().getResourceDescription());
52+
}
53+
printer.printf("and overriding is disabled.");
5054
return description.toString();
5155
}
5256

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/diagnostics/analyzer/BeanDefinitionOverrideFailureAnalyzerTests.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@
1616

1717
package org.springframework.boot.diagnostics.analyzer;
1818

19+
import java.util.function.Supplier;
20+
1921
import org.junit.Test;
2022

2123
import org.springframework.beans.factory.support.BeanDefinitionOverrideException;
2224
import org.springframework.boot.diagnostics.FailureAnalysis;
25+
import org.springframework.context.ApplicationContextInitializer;
2326
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
2427
import org.springframework.context.annotation.Bean;
2528
import org.springframework.context.annotation.Configuration;
@@ -43,6 +46,18 @@ public void analyzeBeanDefinitionOverrideException() {
4346
assertThat(description).contains(FirstConfiguration.class.getName());
4447
}
4548

49+
@Test
50+
public void analyzeBeanDefinitionOverrideExceptionWithDefinitionsWithNoResourceDescription() {
51+
FailureAnalysis analysis = performAnalysis((context) -> {
52+
context.registerBean("testBean", String.class, (Supplier<String>) String::new);
53+
context.registerBean("testBean", String.class, (Supplier<String>) String::new);
54+
});
55+
String description = analysis.getDescription();
56+
assertThat(description)
57+
.isEqualTo("The bean 'testBean' could not be registered. A bean with that name has already"
58+
+ " been defined and overriding is disabled.");
59+
}
60+
4661
private FailureAnalysis performAnalysis(Class<?> configuration) {
4762
BeanDefinitionOverrideException failure = createFailure(configuration);
4863
assertThat(failure).isNotNull();
@@ -63,6 +78,28 @@ private BeanDefinitionOverrideException createFailure(Class<?> configuration) {
6378
}
6479
}
6580

81+
private FailureAnalysis performAnalysis(
82+
ApplicationContextInitializer<AnnotationConfigApplicationContext> initializer) {
83+
BeanDefinitionOverrideException failure = createFailure(initializer);
84+
assertThat(failure).isNotNull();
85+
return new BeanDefinitionOverrideFailureAnalyzer().analyze(failure);
86+
}
87+
88+
private BeanDefinitionOverrideException createFailure(
89+
ApplicationContextInitializer<AnnotationConfigApplicationContext> initializer) {
90+
try {
91+
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
92+
context.setAllowBeanDefinitionOverriding(false);
93+
initializer.initialize(context);
94+
context.refresh();
95+
context.close();
96+
return null;
97+
}
98+
catch (BeanDefinitionOverrideException ex) {
99+
return ex;
100+
}
101+
}
102+
66103
@Configuration
67104
@Import({ FirstConfiguration.class, SecondConfiguration.class })
68105
static class BeanOverrideConfiguration {

0 commit comments

Comments
 (0)