共计 2727 个字符,预计需要花费 7 分钟才能阅读完成。
咱们还以 TransactionServiceTest 为例:
@Service | |
public class TransactionServiceTest { | |
@Autowired | |
private IQrcodeAdScheduleService qrcodeAdScheduleService; | |
} |
图 1
图 2
由图 1,图 2 能够看出 Spring Boot 执行 beanFactory.preInstantiateSingletons() 办法的过程中,须要实例化指标 bean(TransactionServiceTest);
图 3
for (BeanPostProcessor bp : getBeanPostProcessors()) {if (bp instanceof InstantiationAwareBeanPostProcessor) {InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp; | |
//bean 实例化后,进行后置解决 | |
if (!ibp.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName)) { | |
continueWithPropertyPopulation = false; | |
break; | |
} | |
} | |
} |
图 4
图 3,图 4 能够看到是这个 AutowiredAnnotationBeanPostProcessor 是专门来解决 @Autowired 的。
图 5
图 6
图 5,图 6 以看到通过 AutowiredAnnotationBeanPostProcessor 解决之后,接着实例化 被 @Autowired 作用的属性 bean(也就是 qrcodeAdScheduleService)!
那么问题来了 AutowiredAnnotationBeanPostProcessor 是怎么辨认 @Autowired 属性信息的呢?咱们接着往下看:
图 4
图 4 能够分明地看到其中的调用堆栈信息。也就是说这个 applyMergedBeanDefinitionPostProcessors() 是解析 @Autowired 并进行存储的入口
// Allow post-processors to modify the merged bean definition. | |
synchronized (mbd.postProcessingLock) {if (!mbd.postProcessed) { | |
try { | |
// 后置处理器批改 BeanDefinition 构造 | |
applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName); | |
} | |
catch (Throwable ex) {throw new BeanCreationException(mbd.getResourceDescription(), beanName, | |
"Post-processing of merged bean definition failed", ex); | |
} | |
mbd.postProcessed = true; | |
} | |
} |
对于 AutowiredAnnotationBeanPostProcessor 来说时找到指标 bean 外面 @Autowired 注解信息:
public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) { | |
// 解析 @Autowired | |
InjectionMetadata metadata = findAutowiringMetadata(beanName, beanType, null); | |
metadata.checkConfigMembers(beanDefinition); | |
} |
原来,在 AutowiredAnnotationBeanPostProcessor 的构造方法中:
public AutowiredAnnotationBeanPostProcessor() {this.autowiredAnnotationTypes.add(Autowired.class); | |
this.autowiredAnnotationTypes.add(Value.class); | |
try {this.autowiredAnnotationTypes.add((Class<? extends Annotation>) | |
ClassUtils.forName("javax.inject.Inject", AutowiredAnnotationBeanPostProcessor.class.getClassLoader())); | |
logger.info("JSR-330'javax.inject.Inject'annotation found and supported for autowiring"); | |
} | |
catch (ClassNotFoundException ex) {// JSR-330 API not available - simply skip.} | |
} |
autowiredAnnotationTypes 汇合别离保留了 Autowired.class,Value.class 等信息;而后通过 findAutowiredAnnotation() 进行解析,匹配,保留
//ao, 就是咱们通过反射获取 Field 信息 | |
private AnnotationAttributes findAutowiredAnnotation(AccessibleObject ao) {if (ao.getAnnotations().length > 0) { | |
// 遍历 AutowiredAnnotationBeanPostProcessor.autowiredAnnotationTypes 中保留的注解信息 | |
for (Class<? extends Annotation> type : this.autowiredAnnotationTypes) {AnnotationAttributes attributes = AnnotatedElementUtils.getMergedAnnotationAttributes(ao, type); | |
// 如果匹配得上就返回 | |
if (attributes != null) {return attributes;} | |
} | |
} | |
return null; | |
} |
通过下面的办法曾经获取并保留了 @Autowired 相干的信息。OK,最初在 doCreateBean() 重的 populateBean(beanName, mbd, instanceWrapper) 实例化被 @Autowired 作用的 bean!