From ffdd405fb1823acc0948c22945df0a4641c94bea Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Tue, 5 Dec 2023 14:28:28 -0800 Subject: [PATCH] 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 --- .../NoUniqueBeanDefinitionFailureAnalyzer.java | 13 +++++++------ .../NoUniqueBeanDefinitionFailureAnalyzerTests.java | 8 ++++++++ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/diagnostics/analyzer/NoUniqueBeanDefinitionFailureAnalyzer.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/diagnostics/analyzer/NoUniqueBeanDefinitionFailureAnalyzer.java index 77c37fb0ad2..dee8f8eb9fd 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/diagnostics/analyzer/NoUniqueBeanDefinitionFailureAnalyzer.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/diagnostics/analyzer/NoUniqueBeanDefinitionFailureAnalyzer.java @@ -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)); } } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/diagnostics/analyzer/NoUniqueBeanDefinitionFailureAnalyzerTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/diagnostics/analyzer/NoUniqueBeanDefinitionFailureAnalyzerTests.java index 35943e7ccf6..e8600faabe2 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/diagnostics/analyzer/NoUniqueBeanDefinitionFailureAnalyzerTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/diagnostics/analyzer/NoUniqueBeanDefinitionFailureAnalyzerTests.java @@ -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);