先来一张图:
大致流程是:容器刷新时,调用refresh()方法,然后在运行到finishBeanFactoryInitialization()的时候,会调用到preInstantiateSingletons()去获取Bean。getBean()方法获取Bean时,首先看看缓存有没有,如果没有就会调用创建Bean的流程。创建Bean的流程里,在populateBean()之前,applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);方法会调用AutowiredAnnotationBeanPostProcessor.postProcessMergedBeanDefinition()方法把被@Autowired标注的属性放到AutowiredAnnotationBeanPostProcessor的this.injectionMetadataCache缓存里,即上图第一行代码所做工作。
private InjectionMetadata findAutowiringMetadata(String beanName, Class<?> clazz, @Nullable PropertyValues pvs) {
// Fall back to class name as cache key, for backwards compatibility with custom callers.
String cacheKey = (StringUtils.ha