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 555df79bce1..77c37fb0ad2 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 @@ -1,5 +1,5 @@ /* - * Copyright 2012-2022 the original author or authors. + * Copyright 2012-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -76,9 +76,14 @@ class NoUniqueBeanDefinitionFailureAnalyzer extends AbstractInjectionFailureAnal private String getDefinitionDescription(String beanName, BeanDefinition definition) { if (StringUtils.hasText(definition.getFactoryMethodName())) { return String.format("\t- %s: defined by method '%s' in %s%n", beanName, definition.getFactoryMethodName(), - definition.getResourceDescription()); + getResourceDescription(definition)); } - return String.format("\t- %s: defined in %s%n", beanName, definition.getResourceDescription()); + return String.format("\t- %s: defined in %s%n", beanName, getResourceDescription(definition)); + } + + private String getResourceDescription(BeanDefinition definition) { + String resourceDescription = definition.getResourceDescription(); + return (resourceDescription != null) ? resourceDescription : "unknown location"; } private String[] extractBeanNames(NoUniqueBeanDefinitionException cause) { 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 b14fcaa76dd..3950d036073 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 @@ -1,5 +1,5 @@ /* - * Copyright 2012-2022 the original author or authors. + * Copyright 2012-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -94,6 +94,7 @@ class NoUniqueBeanDefinitionFailureAnalyzerTests { } private BeanCreationException createFailure(Class consumer) { + this.context.registerBean("beanOne", TestBean.class); this.context.register(DuplicateBeansProducer.class, consumer); this.context.setParent(new AnnotationConfigApplicationContext(ParentProducer.class)); try { @@ -110,8 +111,7 @@ class NoUniqueBeanDefinitionFailureAnalyzerTests { } private void assertFoundBeans(FailureAnalysis analysis) { - assertThat(analysis.getDescription()) - .contains("beanOne: defined by method 'beanOne' in " + DuplicateBeansProducer.class.getName()); + assertThat(analysis.getDescription()).contains("beanOne: defined in unknown location"); assertThat(analysis.getDescription()) .contains("beanTwo: defined by method 'beanTwo' in " + DuplicateBeansProducer.class.getName()); assertThat(analysis.getDescription()) @@ -126,11 +126,6 @@ class NoUniqueBeanDefinitionFailureAnalyzerTests { @ImportResource("/org/springframework/boot/diagnostics/analyzer/nounique/producer.xml") static class DuplicateBeansProducer { - @Bean - TestBean beanOne() { - return new TestBean(); - } - @Bean TestBean beanTwo() { return new TestBean();