Skip to content

Commit 1cdd56b

Browse files
committed
Log multiple primary bean detection in DefaultListableBeanFactory
Prior to this commit, a NoUniqueBeanDefinitionException was thrown when multiple primary beans were detected within a given set of beans, but nothing was logged. For use cases where the exception is handled by infrastructure code, it may not be obvious to the developer what the problem is. To address that, a TRACE message is now logged whenever multiple competing primary beans are detected in DefaultListableBeanFactory. Closes gh-35550
1 parent 3041071 commit 1cdd56b

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2098,8 +2098,9 @@ protected String determinePrimaryCandidate(Map<String, Object> candidates, Class
20982098
boolean candidateLocal = containsBeanDefinition(candidateBeanName);
20992099
boolean primaryLocal = containsBeanDefinition(primaryBeanName);
21002100
if (candidateLocal == primaryLocal) {
2101-
throw new NoUniqueBeanDefinitionException(requiredType, candidates.size(),
2102-
"more than one 'primary' bean found among candidates: " + candidates.keySet());
2101+
String message = "more than one 'primary' bean found among candidates: " + candidates.keySet();
2102+
logger.trace(message);
2103+
throw new NoUniqueBeanDefinitionException(requiredType, candidates.size(), message);
21032104
}
21042105
else if (candidateLocal) {
21052106
primaryBeanName = candidateBeanName;

spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1798,7 +1798,7 @@ void getBeanByTypeWithMultiplePrimary() {
17981798

17991799
assertThatExceptionOfType(NoUniqueBeanDefinitionException.class)
18001800
.isThrownBy(() -> lbf.getBean(TestBean.class))
1801-
.withMessageContaining("more than one 'primary'");
1801+
.withMessageEndingWith("more than one 'primary' bean found among candidates: [bd1, bd2]");
18021802
}
18031803

18041804
@Test
@@ -2122,7 +2122,7 @@ void getBeanByTypeInstanceWithMultiplePrimary() {
21222122

21232123
assertThatExceptionOfType(NoUniqueBeanDefinitionException.class)
21242124
.isThrownBy(() -> lbf.getBean(ConstructorDependency.class, 42))
2125-
.withMessageContaining("more than one 'primary'");
2125+
.withMessageEndingWith("more than one 'primary' bean found among candidates: [bd1, bd2]");
21262126
}
21272127

21282128
@Test

0 commit comments

Comments
 (0)