Created
June 30, 2020 07:44
-
-
Save circlee/5022e6752547b712fc5b6dcf5cdadae8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protected Map<String, Object> findAutowireCandidates( | |
@Nullable String beanName, Class<?> requiredType, DependencyDescriptor descriptor) { | |
String[] candidateNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors( | |
this, requiredType, true, descriptor.isEager()); | |
Map<String, Object> result = new LinkedHashMap<>(candidateNames.length); | |
for (Class<?> autowiringType : this.resolvableDependencies.keySet()) { | |
if (autowiringType.isAssignableFrom(requiredType)) { | |
Object autowiringValue = this.resolvableDependencies.get(autowiringType); | |
autowiringValue = AutowireUtils.resolveAutowiringValue(autowiringValue, requiredType); | |
if (requiredType.isInstance(autowiringValue)) { | |
result.put(ObjectUtils.identityToString(autowiringValue), autowiringValue); | |
break; | |
} | |
} | |
} | |
for (String candidate : candidateNames) { | |
if (!isSelfReference(beanName, candidate) && isAutowireCandidate(candidate, descriptor)) { | |
addCandidateEntry(result, candidate, descriptor, requiredType); | |
} | |
} | |
if (result.isEmpty() && !indicatesMultipleBeans(requiredType)) { | |
// Consider fallback matches if the first pass failed to find anything... | |
DependencyDescriptor fallbackDescriptor = descriptor.forFallbackMatch(); | |
for (String candidate : candidateNames) { | |
if (!isSelfReference(beanName, candidate) && isAutowireCandidate(candidate, fallbackDescriptor)) { | |
addCandidateEntry(result, candidate, descriptor, requiredType); | |
} | |
} | |
if (result.isEmpty()) { | |
// Consider self references as a final pass... | |
// but in the case of a dependency collection, not the very same bean itself. | |
for (String candidate : candidateNames) { | |
if (isSelfReference(beanName, candidate) && | |
(!(descriptor instanceof MultiElementDescriptor) || !beanName.equals(candidate)) && | |
isAutowireCandidate(candidate, fallbackDescriptor)) { | |
addCandidateEntry(result, candidate, descriptor, requiredType); | |
} | |
} | |
} | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment