我们还以TransactionServiceTest为例:

@Service
public class TransactionServiceTest {
    @Autowired
    private IQrcodeAdScheduleService qrcodeAdScheduleService;
}

image.png图1
image.png图2
由图1,图2可以看出Spring Boot执行beanFactory.preInstantiateSingletons()方法的过程中,需要实例化目标 bean(TransactionServiceTest);
image.png
图3
其中AbstractAutowireCapableBeanFactory.populateBean():

            if (hasInstAwareBpps) {
                for (BeanPostProcessor bp : getBeanPostProcessors()) {
                    if (bp instanceof InstantiationAwareBeanPostProcessor) {
                        InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
                        //调用AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues()方法;
                        pvs = ibp.postProcessPropertyValues(pvs, filteredPds, bw.getWrappedInstance(), beanName);
                        if (pvs == null) {
                            return;
                        }
                    }
                }
            }

image.png
图4
我们了解到上面的逻辑其实是通过AutowiredAnnotationBeanPostProcessor.inject()方法->DefaultListableBeanFactory.resolveDependency()实实例化 被@Autowired作用的属性bean(也就是qrcodeAdScheduleService) ;
图3,图4可以看到是这个AutowiredAnnotationBeanPostProcessor是专门来处理@Autowired的。

image.png
图5
image.png
图6

图5,图6以看到通过AutowiredAnnotationBeanPostProcessor处理之后,接着实例化 被@Autowired作用的属性bean(也就是qrcodeAdScheduleService)!

那么问题来了AutowiredAnnotationBeanPostProcessor是怎么识别@Autowired属性信息的呢?我们接着往下看:

image.png图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!


子瞻
1 声望9 粉丝