From 1cdd56bf024845553d78e574dfe420cd04912998 Mon Sep 17 00:00:00 2001 From: Sam Brannen <104798+sbrannen@users.noreply.github.com> Date: Fri, 3 Oct 2025 13:57:01 +0200 Subject: [PATCH] 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 --- .../beans/factory/support/DefaultListableBeanFactory.java | 5 +++-- .../beans/factory/DefaultListableBeanFactoryTests.java | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java index 2c67f665511..80e24655bf1 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java @@ -2098,8 +2098,9 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto boolean candidateLocal = containsBeanDefinition(candidateBeanName); boolean primaryLocal = containsBeanDefinition(primaryBeanName); if (candidateLocal == primaryLocal) { - throw new NoUniqueBeanDefinitionException(requiredType, candidates.size(), - "more than one 'primary' bean found among candidates: " + candidates.keySet()); + String message = "more than one 'primary' bean found among candidates: " + candidates.keySet(); + logger.trace(message); + throw new NoUniqueBeanDefinitionException(requiredType, candidates.size(), message); } else if (candidateLocal) { primaryBeanName = candidateBeanName; diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java index dc964ad5ff4..c85c9493216 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java @@ -1798,7 +1798,7 @@ class DefaultListableBeanFactoryTests { assertThatExceptionOfType(NoUniqueBeanDefinitionException.class) .isThrownBy(() -> lbf.getBean(TestBean.class)) - .withMessageContaining("more than one 'primary'"); + .withMessageEndingWith("more than one 'primary' bean found among candidates: [bd1, bd2]"); } @Test @@ -2122,7 +2122,7 @@ class DefaultListableBeanFactoryTests { assertThatExceptionOfType(NoUniqueBeanDefinitionException.class) .isThrownBy(() -> lbf.getBean(ConstructorDependency.class, 42)) - .withMessageContaining("more than one 'primary'"); + .withMessageEndingWith("more than one 'primary' bean found among candidates: [bd1, bd2]"); } @Test