Merge branch '6.2.x'

This commit is contained in:
Sam Brannen
2024-11-21 09:53:46 +01:00
2 changed files with 108 additions and 0 deletions

View File

@@ -331,6 +331,12 @@ class BeanOverrideBeanFactoryPostProcessor implements BeanFactoryPostProcessor,
return beanNames;
}
/**
* Determine the primary candidate in the given set of bean names.
* <p>Honors both <em>primary</em> and <em>fallback</em> semantics.
* @return the name of the primary candidate, or {@code null} if none found
* @see org.springframework.beans.factory.support.DefaultListableBeanFactory#determinePrimaryCandidate(Map, Class)
*/
@Nullable
private static String determinePrimaryCandidate(
ConfigurableListableBeanFactory beanFactory, Set<String> candidateBeanNames, Class<?> beanType) {
@@ -340,6 +346,7 @@ class BeanOverrideBeanFactoryPostProcessor implements BeanFactoryPostProcessor,
}
String primaryBeanName = null;
// First pass: identify unique primary candidate
for (String candidateBeanName : candidateBeanNames) {
if (beanFactory.containsBeanDefinition(candidateBeanName)) {
BeanDefinition beanDefinition = beanFactory.getBeanDefinition(candidateBeanName);
@@ -352,6 +359,21 @@ class BeanOverrideBeanFactoryPostProcessor implements BeanFactoryPostProcessor,
}
}
}
// Second pass: identify unique non-fallback candidate
if (primaryBeanName == null) {
for (String candidateBeanName : candidateBeanNames) {
if (beanFactory.containsBeanDefinition(candidateBeanName)) {
BeanDefinition beanDefinition = beanFactory.getBeanDefinition(candidateBeanName);
if (!beanDefinition.isFallback()) {
if (primaryBeanName != null) {
// More than one non-fallback bean found among candidates.
return null;
}
primaryBeanName = candidateBeanName;
}
}
}
}
return primaryBeanName;
}