Skip to content

Commit b5cebed

Browse files
committed
Improve failure description when bean def has no resource description
Closes gh-33765
1 parent c939e27 commit b5cebed

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -76,9 +76,14 @@ private void buildMessage(StringBuilder message, String beanName) {
7676
private String getDefinitionDescription(String beanName, BeanDefinition definition) {
7777
if (StringUtils.hasText(definition.getFactoryMethodName())) {
7878
return String.format("\t- %s: defined by method '%s' in %s%n", beanName, definition.getFactoryMethodName(),
79-
definition.getResourceDescription());
79+
getResourceDescription(definition));
8080
}
81-
return String.format("\t- %s: defined in %s%n", beanName, definition.getResourceDescription());
81+
return String.format("\t- %s: defined in %s%n", beanName, getResourceDescription(definition));
82+
}
83+
84+
private String getResourceDescription(BeanDefinition definition) {
85+
String resourceDescription = definition.getResourceDescription();
86+
return (resourceDescription != null) ? resourceDescription : "unknown location";
8287
}
8388

8489
private String[] extractBeanNames(NoUniqueBeanDefinitionException cause) {

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -94,6 +94,7 @@ void failureAnalysisForObjectProviderConstructorConsumer() {
9494
}
9595

9696
private BeanCreationException createFailure(Class<?> consumer) {
97+
this.context.registerBean("beanOne", TestBean.class);
9798
this.context.register(DuplicateBeansProducer.class, consumer);
9899
this.context.setParent(new AnnotationConfigApplicationContext(ParentProducer.class));
99100
try {
@@ -110,8 +111,7 @@ private FailureAnalysis analyzeFailure(BeanCreationException failure) {
110111
}
111112

112113
private void assertFoundBeans(FailureAnalysis analysis) {
113-
assertThat(analysis.getDescription())
114-
.contains("beanOne: defined by method 'beanOne' in " + DuplicateBeansProducer.class.getName());
114+
assertThat(analysis.getDescription()).contains("beanOne: defined in unknown location");
115115
assertThat(analysis.getDescription())
116116
.contains("beanTwo: defined by method 'beanTwo' in " + DuplicateBeansProducer.class.getName());
117117
assertThat(analysis.getDescription())
@@ -126,11 +126,6 @@ private void assertFoundBeans(FailureAnalysis analysis) {
126126
@ImportResource("/org/springframework/boot/diagnostics/analyzer/nounique/producer.xml")
127127
static class DuplicateBeansProducer {
128128

129-
@Bean
130-
TestBean beanOne() {
131-
return new TestBean();
132-
}
133-
134129
@Bean
135130
TestBean beanTwo() {
136131
return new TestBean();

0 commit comments

Comments
 (0)