SpringBoot里实现了某个接口的实现类运行时如何注入的?
2023-09-14 09:03:08 时间
待注入的代码位置:
elementType:
when is this hook injected?
Candidates是在这个方法里解析出来的:protected Map<String, Object> findAutowireCandidates
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 (Map.Entry<Class<?>, Object> classObjectEntry : this.resolvableDependencies.entrySet()) {
Class<?> autowiringType = classObjectEntry.getKey();
if (autowiringType.isAssignableFrom(requiredType)) {
Object autowiringValue = classObjectEntry.getValue();
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()) {
boolean multiple = 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) &&
(!multiple || getAutowireCandidateResolver().hasQualifier(descriptor))) {
addCandidateEntry(result, candidate, descriptor, requiredType);
}
}
if (result.isEmpty() && !multiple) {
// 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;
}
要获取更多Jerry的原创文章,请关注公众号"汪子熙":
相关文章
- springboot注入一个jar包里的bean
- springboot 使用secheduled任务调度
- springboot @PropertySource+@Value注入properties配置文件属性值
- Atitit java解析yml文件 以及使用 spel ognl 读取 v4 u77.docx Atitit java解析yml文件 目录 1.1. Springboot use snak
- springboot 多线程 @EnableAsync @Async使用总结
- Springboot扩展点之BeanPostProcessor
- 补习系列-springboot-使用assembly进行项目打包
- SpringBoot——实现WebService接口服务端以及客户端开发
- springboot中service层注入的是实现类,但Controller层接收的是接口
- SpringBoot如何自定义启动的Banner 在线生成SpringBoot启动的Banner工具 如何使用在线生成工具生成的SpringBoot的Banner
- SpringBoot配置分析、获取到SpringBoot配置文件信息以及几种获取配置文件信息的方式
- SpringBoot聚合工程集成MyBatis报错:Invalid bound statement (not found): com.xxx.xxx.test.TestService.getAll
- 【微服务】SpringBoot启动流程初始化OpenFeign的入口
- Springboot扩展点之DisposableBean
- IntelliJ IDEA 创建 SpringBoot 项目 Maven 打包 jar