From bc01e3116f422aeab9ce00c4bf7a4a563d6a0ff6 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 20 Feb 2024 13:51:37 +0100 Subject: [PATCH] Ignore fallback bean for shortcut resolution See gh-26241 See gh-28122 --- .../support/DefaultListableBeanFactory.java | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 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 67c9142e5d70..c7505a4f2468 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 @@ -1408,7 +1408,8 @@ public Object doResolveDependency(DependencyDescriptor descriptor, @Nullable Str } if (dependencyName != null && isTypeMatch(dependencyName, type) && isAutowireCandidate(dependencyName, descriptor) && - !hasPrimaryConflict(dependencyName, type) && !isSelfReference(beanName, dependencyName)) { + !isFallback(dependencyName) && !hasPrimaryConflict(dependencyName, type) && + !isSelfReference(beanName, dependencyName)) { if (autowiredBeanNames != null) { autowiredBeanNames.add(dependencyName); } @@ -1819,10 +1820,8 @@ else if (candidateLocal) { } // Second pass: identify unique non-fallback candidate if (primaryBeanName == null) { - for (Map.Entry entry : candidates.entrySet()) { - String candidateBeanName = entry.getKey(); - Object beanInstance = entry.getValue(); - if (!isFallback(candidateBeanName, beanInstance)) { + for (String candidateBeanName : candidates.keySet()) { + if (!isFallback(candidateBeanName)) { if (primaryBeanName != null) { return null; } @@ -1896,17 +1895,15 @@ protected boolean isPrimary(String beanName, Object beanInstance) { * Return whether the bean definition for the given bean name has been * marked as a fallback bean. * @param beanName the name of the bean - * @param beanInstance the corresponding bean instance (can be {@code null}) - * @return whether the given bean qualifies as fallback * @since 6.2 */ - protected boolean isFallback(String beanName, Object beanInstance) { + private boolean isFallback(String beanName) { String transformedBeanName = transformedBeanName(beanName); if (containsBeanDefinition(transformedBeanName)) { return getMergedLocalBeanDefinition(transformedBeanName).isFallback(); } return (getParentBeanFactory() instanceof DefaultListableBeanFactory parent && - parent.isFallback(transformedBeanName, beanInstance)); + parent.isFallback(transformedBeanName)); } /** @@ -1954,7 +1951,6 @@ private boolean isSelfReference(@Nullable String beanName, @Nullable String cand * Determine whether there is a primary bean registered for the given dependency type, * not matching the given bean name. */ - @Nullable private boolean hasPrimaryConflict(String beanName, Class dependencyType) { for (String candidate : this.primaryBeanNames) { if (isTypeMatch(candidate, dependencyType) && !candidate.equals(beanName)) {