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
This commit is contained in:
Phillip Webb 2023-12-05 14:28:28 -08:00
parent ce7d384d2c
commit ffdd405fb1
2 changed files with 15 additions and 6 deletions

View File

@ -56,11 +56,12 @@ class NoUniqueBeanDefinitionFailureAnalyzer extends AbstractInjectionFailureAnal
for (String beanName : beanNames) {
buildMessage(message, beanName);
}
return new FailureAnalysis(message.toString(),
"Consider marking one of the beans as @Primary, updating the consumer to"
+ " accept multiple beans, or using @Qualifier to identify the"
+ " bean that should be consumed",
cause);
MissingParameterNamesFailureAnalyzer.appendPossibility(message);
StringBuilder action = new StringBuilder(
"Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, "
+ "or using @Qualifier to identify the bean that should be consumed");
action.append("%n%n%s".formatted(MissingParameterNamesFailureAnalyzer.ACTION));
return new FailureAnalysis(message.toString(), action.toString(), cause);
}
private void buildMessage(StringBuilder message, String beanName) {
@ -69,7 +70,7 @@ class NoUniqueBeanDefinitionFailureAnalyzer extends AbstractInjectionFailureAnal
message.append(getDefinitionDescription(beanName, definition));
}
catch (NoSuchBeanDefinitionException ex) {
message.append(String.format("\t- %s: a programmatically registered singleton", beanName));
message.append(String.format("\t- %s: a programmatically registered singleton%n", beanName));
}
}

View File

@ -93,6 +93,14 @@ class NoUniqueBeanDefinitionFailureAnalyzerTests {
assertFoundBeans(failureAnalysis);
}
@Test
void failureAnalysisIncludesPossiblyMissingParameterNames() {
FailureAnalysis failureAnalysis = analyzeFailure(createFailure(MethodConsumer.class));
assertThat(failureAnalysis.getDescription()).contains(MissingParameterNamesFailureAnalyzer.POSSIBILITY);
assertThat(failureAnalysis.getAction()).contains(MissingParameterNamesFailureAnalyzer.ACTION);
assertFoundBeans(failureAnalysis);
}
private BeanCreationException createFailure(Class<?> consumer) {
this.context.registerBean("beanOne", TestBean.class);
this.context.register(DuplicateBeansProducer.class, consumer);