|
118 | 118 | * @author Phillip Webb
|
119 | 119 | * @author Stephane Nicoll
|
120 | 120 | * @author Sebastien Deleuze
|
| 121 | + * @author Yanming Zhou |
121 | 122 | * @since 16 April 2001
|
122 | 123 | * @see #registerBeanDefinition
|
123 | 124 | * @see #addBeanPostProcessor
|
@@ -1479,6 +1480,9 @@ else if (candidateNames.length > 1) {
|
1479 | 1480 | if (candidateName == null) {
|
1480 | 1481 | candidateName = determineHighestPriorityCandidate(candidates, requiredType.toClass());
|
1481 | 1482 | }
|
| 1483 | + if (candidateName == null) { |
| 1484 | + candidateName = determineEffectivelyPrimaryCandidate(candidates, requiredType.toClass()); |
| 1485 | + } |
1482 | 1486 | if (candidateName != null) {
|
1483 | 1487 | Object beanInstance = candidates.get(candidateName);
|
1484 | 1488 | if (beanInstance == null) {
|
@@ -2046,6 +2050,34 @@ else if (candidatePriority < highestPriority) {
|
2046 | 2050 | return highestPriorityBeanName;
|
2047 | 2051 | }
|
2048 | 2052 |
|
| 2053 | + /** |
| 2054 | + * Determine the candidate which is the only one default candidate in the given set of beans. |
| 2055 | + * @param candidates a Map of candidate names and candidate instances |
| 2056 | + * (or candidate classes if not created yet) that match the required type |
| 2057 | + * @param requiredType the target dependency type to match against |
| 2058 | + * @return the name of the only one default candidate, |
| 2059 | + * or {@code null} if none found |
| 2060 | + */ |
| 2061 | + @Nullable |
| 2062 | + protected String determineEffectivelyPrimaryCandidate(Map<String, Object> candidates, Class<?> requiredType) { |
| 2063 | + String candidateBeanName = null; |
| 2064 | + for (Map.Entry<String, Object> entry : candidates.entrySet()) { |
| 2065 | + String transformedBeanName = transformedBeanName(entry.getKey()); |
| 2066 | + if (containsBeanDefinition(transformedBeanName)) { |
| 2067 | + RootBeanDefinition bd = getMergedLocalBeanDefinition(transformedBeanName); |
| 2068 | + if (bd.isAutowireCandidate() && bd.isDefaultCandidate()) { |
| 2069 | + if (candidateBeanName == null) { |
| 2070 | + candidateBeanName = entry.getKey(); |
| 2071 | + } |
| 2072 | + else { |
| 2073 | + return null; |
| 2074 | + } |
| 2075 | + } |
| 2076 | + } |
| 2077 | + } |
| 2078 | + return candidateBeanName; |
| 2079 | + } |
| 2080 | + |
2049 | 2081 | /**
|
2050 | 2082 | * Return whether the bean definition for the given bean name has been
|
2051 | 2083 | * marked as a primary bean.
|
|
0 commit comments