Merge branch '2.1.x'

Closes gh-18749
pull/18750/head
Andy Wilkinson 5 years ago
commit 533ca0624f

@ -42,11 +42,15 @@ class BeanDefinitionOverrideFailureAnalyzer extends AbstractFailureAnalyzer<Bean
private String getDescription(BeanDefinitionOverrideException ex) { private String getDescription(BeanDefinitionOverrideException ex) {
StringWriter description = new StringWriter(); StringWriter description = new StringWriter();
PrintWriter printer = new PrintWriter(description); PrintWriter printer = new PrintWriter(description);
printer.printf( printer.printf("The bean '%s'", ex.getBeanName());
"The bean '%s', defined in %s, could not be registered. A bean with that " if (ex.getBeanDefinition().getResourceDescription() != null) {
+ "name has already been defined in %s and overriding is disabled.", printer.printf(", defined in %s,", ex.getBeanDefinition().getResourceDescription());
ex.getBeanName(), ex.getBeanDefinition().getResourceDescription(), }
ex.getExistingDefinition().getResourceDescription()); printer.printf(" could not be registered. A bean with that name has already been defined ");
if (ex.getExistingDefinition().getResourceDescription() != null) {
printer.printf("in %s ", ex.getExistingDefinition().getResourceDescription());
}
printer.printf("and overriding is disabled.");
return description.toString(); return description.toString();
} }

@ -16,10 +16,13 @@
package org.springframework.boot.diagnostics.analyzer; package org.springframework.boot.diagnostics.analyzer;
import java.util.function.Supplier;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.support.BeanDefinitionOverrideException; import org.springframework.beans.factory.support.BeanDefinitionOverrideException;
import org.springframework.boot.diagnostics.FailureAnalysis; import org.springframework.boot.diagnostics.FailureAnalysis;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@ -43,6 +46,18 @@ class BeanDefinitionOverrideFailureAnalyzerTests {
assertThat(description).contains(FirstConfiguration.class.getName()); assertThat(description).contains(FirstConfiguration.class.getName());
} }
@Test
void analyzeBeanDefinitionOverrideExceptionWithDefinitionsWithNoResourceDescription() {
FailureAnalysis analysis = performAnalysis((context) -> {
context.registerBean("testBean", String.class, (Supplier<String>) String::new);
context.registerBean("testBean", String.class, (Supplier<String>) String::new);
});
String description = analysis.getDescription();
assertThat(description)
.isEqualTo("The bean 'testBean' could not be registered. A bean with that name has already"
+ " been defined and overriding is disabled.");
}
private FailureAnalysis performAnalysis(Class<?> configuration) { private FailureAnalysis performAnalysis(Class<?> configuration) {
BeanDefinitionOverrideException failure = createFailure(configuration); BeanDefinitionOverrideException failure = createFailure(configuration);
assertThat(failure).isNotNull(); assertThat(failure).isNotNull();
@ -63,6 +78,28 @@ class BeanDefinitionOverrideFailureAnalyzerTests {
} }
} }
private FailureAnalysis performAnalysis(
ApplicationContextInitializer<AnnotationConfigApplicationContext> initializer) {
BeanDefinitionOverrideException failure = createFailure(initializer);
assertThat(failure).isNotNull();
return new BeanDefinitionOverrideFailureAnalyzer().analyze(failure);
}
private BeanDefinitionOverrideException createFailure(
ApplicationContextInitializer<AnnotationConfigApplicationContext> initializer) {
try {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.setAllowBeanDefinitionOverriding(false);
initializer.initialize(context);
context.refresh();
context.close();
return null;
}
catch (BeanDefinitionOverrideException ex) {
return ex;
}
}
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
@Import({ FirstConfiguration.class, SecondConfiguration.class }) @Import({ FirstConfiguration.class, SecondConfiguration.class })
static class BeanOverrideConfiguration { static class BeanOverrideConfiguration {

Loading…
Cancel
Save