mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
Improve failure description when bean def has no resource description
Closes gh-33765
This commit is contained in:
parent
c939e27925
commit
b5cebed120
@ -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) {
|
||||
|
@ -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();
|
||||
|
Loading…
Reference in New Issue
Block a user