Skip to content

Commit ffdd405

Browse files
committed
Update NoUniqueBeanDefinitionFailureAnalyzer with parameter hints
Add addition description and action text to help point to the fact that the `NoUniqueBeanDefinitionException` can be thrown due to a missing `-parameters` compiler setting. Closes gh-38652
1 parent ce7d384 commit ffdd405

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,12 @@ protected FailureAnalysis analyze(Throwable rootFailure, NoUniqueBeanDefinitionE
5656
for (String beanName : beanNames) {
5757
buildMessage(message, beanName);
5858
}
59-
return new FailureAnalysis(message.toString(),
60-
"Consider marking one of the beans as @Primary, updating the consumer to"
61-
+ " accept multiple beans, or using @Qualifier to identify the"
62-
+ " bean that should be consumed",
63-
cause);
59+
MissingParameterNamesFailureAnalyzer.appendPossibility(message);
60+
StringBuilder action = new StringBuilder(
61+
"Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, "
62+
+ "or using @Qualifier to identify the bean that should be consumed");
63+
action.append("%n%n%s".formatted(MissingParameterNamesFailureAnalyzer.ACTION));
64+
return new FailureAnalysis(message.toString(), action.toString(), cause);
6465
}
6566

6667
private void buildMessage(StringBuilder message, String beanName) {
@@ -69,7 +70,7 @@ private void buildMessage(StringBuilder message, String beanName) {
6970
message.append(getDefinitionDescription(beanName, definition));
7071
}
7172
catch (NoSuchBeanDefinitionException ex) {
72-
message.append(String.format("\t- %s: a programmatically registered singleton", beanName));
73+
message.append(String.format("\t- %s: a programmatically registered singleton%n", beanName));
7374
}
7475
}
7576

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ void failureAnalysisForObjectProviderConstructorConsumer() {
9393
assertFoundBeans(failureAnalysis);
9494
}
9595

96+
@Test
97+
void failureAnalysisIncludesPossiblyMissingParameterNames() {
98+
FailureAnalysis failureAnalysis = analyzeFailure(createFailure(MethodConsumer.class));
99+
assertThat(failureAnalysis.getDescription()).contains(MissingParameterNamesFailureAnalyzer.POSSIBILITY);
100+
assertThat(failureAnalysis.getAction()).contains(MissingParameterNamesFailureAnalyzer.ACTION);
101+
assertFoundBeans(failureAnalysis);
102+
}
103+
96104
private BeanCreationException createFailure(Class<?> consumer) {
97105
this.context.registerBean("beanOne", TestBean.class);
98106
this.context.register(DuplicateBeansProducer.class, consumer);

0 commit comments

Comments
 (0)