@Autowired
注解的实现过程,其实就是Spring Bean的自动装配过程。通过看@Autowired源码注释部分我们可以看到@Autowired
的实现是通过AutowiredAnnotationBeanPostProcessor
后置处理器中实现的。
AutowiredAnnotationBeanPostProcessor
后置处理器的执行优先级AutowiredAnnotationBeanPostProcessor
可以直接通过BeanFactory
获取容器中的Bean在分析自动装配前我们先来介绍一下上面的这些后置处理器。
BeanPostProcessor有两个方法,postProcessBeforeInitialization
和postProcessAfterInitialization
。它们分别在任何bean初始化回调之前或之后执行(例如InitializingBean的afterPropertiesSet方法或自定义init-method方法之前或者之后), 在这个时候该bean的属性值已经填充完成了,并且我们返回的bean实例可能已经是原始实例的包装类型了。例如返回一个FactoryBean
。
InstantiationAwareBeanPostProcessor
继承自BeanPostProcessor
接口。主要多提供了以下三个方法。
该方法是在Bean实例化目标对象之前调用,返回的Bean对象可以代理目标,从而有效的阻止了目标Bean的默认实例化。
- protected Object resolveBeforeInstantiation(String beanName, RootBeanDefinition mbd) {
- Object bean = null;
- if (!Boolean.FALSE.equals(mbd.beforeInstantiationResolved)) {
- // Make sure bean class is actually resolved at this point.
- if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
- Class> targetType = determineTargetType(beanName, mbd);
- if (targetType != null) {
- bean = applyBeanPostProcessorsBeforeInstantiation(targetType, beanName);
- if (bean != null) {
- // 如果此方法返回一个非null对象,则Bean创建过程将被短路。
- // 唯一应用的进一步处理是来自已配置BeanPostProcessors的postProcessAfterInitialization回调
- bean = applyBeanPostProcessorsAfterInitialization(bean, beanName);
- }
- }
- }
- mbd.beforeInstantiationResolved = (bean != null);
- }
- return bean;
- }
-
- protected Object applyBeanPostProcessorsBeforeInstantiation(Class> beanClass, String beanName) {
- for (BeanPostProcessor bp : getBeanPostProcessors()) {
- if (bp instanceof InstantiationAwareBeanPostProcessor) {
- InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
- // 执行Bean实例化目标对象之前的后置处理方法
- Object result = ibp.postProcessBeforeInstantiation(beanClass, beanName);
- if (result != null) {
- return result;
- }
- }
- }
- return null;
- }
跟进源码我们可以看出,如果此方法返回一个非null对象,则Bean创建过程将被短路。唯一应用的进一步处理是来自已配置BeanPostProcessors的postProcessAfterInitialization回调。
AutowiredAnnotationBeanPostProcessor
后置处理器主要负责对添加了@Autowired和@Value注解的元素实现自动装配。所以找到需要自动装配的元素,其实就是对@Autowired和@Value注解的解析。
AutowiredAnnotationBeanPostProcessor
后置处理器,找出需要自动装配的元素是在MergedBeanDefinitionPostProcessor.postProcessMergedBeanDefinition
这个方法中实现的;
- doWith:445, AutowiredAnnotationBeanPostProcessor$2 (org.springframework.beans.factory.annotation)
- doWithLocalFields:657, ReflectionUtils (org.springframework.util)
- buildAutowiringMetadata:433, AutowiredAnnotationBeanPostProcessor (org.springframework.beans.factory.annotation)
- findAutowiringMetadata:412, AutowiredAnnotationBeanPostProcessor (org.springframework.beans.factory.annotation)
- postProcessMergedBeanDefinition:235, AutowiredAnnotationBeanPostProcessor (org.springframework.beans.factory.annotation)
- applyMergedBeanDefinitionPostProcessors:1000, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support)
- doCreateBean:523, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support)
- createBean:483, AbstractAutowireCapableBeanFactory (org.springframework.beans.factory.support)
- getObject:312, AbstractBeanFactory$1 (org.springframework.beans.factory.support)
- getSingleton:230, DefaultSingletonBeanRegistry (org.springframework.beans.factory.support)
- doGetBean:308, AbstractBeanFactory (org.springframework.beans.factory.support)
- getBean:197, AbstractBeanFactory (org.springframework.beans.factory.support)
- preInstantiateSingletons:761, DefaultListableBeanFactory (org.springframework.beans.factory.support)
- finishBeanFactoryInitialization:867, AbstractApplicationContext (org.springframework.context.support)
- refresh:543, AbstractApplicationContext (org.springframework.context.support)
:84, AnnotationConfigApplicationContext (org.springframework.context.annotation)
从链路上我们可以看到,找到需要自动装配的元素是在findAutowiringMetadata
方法中实现的,该方法会去调用buildAutowiringMetadata
方法构建元数据信息。如果注解被加载属性上将会被封装成AutowiredFieldElement
对象;如果注解加在方法上,那么元素会被封装成AutowiredMethodElement
对象。这里两个对象的inject
方法将最后完成属性值的注入,主要区别就是使用反射注入值的方式不一样。源码如下:
- private InjectionMetadata buildAutowiringMetadata(final Class> clazz) {
- LinkedList
elements = new LinkedList(); - Class> targetClass = clazz;
-
- do {
- // 存放我们找到的元数据信息
- final LinkedList
currElements = - new LinkedList
(); -
- // 通过反射找出对应Class对象的所有Field
- ReflectionUtils.doWithLocalFields(targetClass, new ReflectionUtils.FieldCallback() {
- @Override
- public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
- // 通过反射找到该字段上所有的注解信息,并判断是否有@Autowired和@Value注解,如果有就将该字段封成AutowiredFieldElement对象
- AnnotationAttributes ann = findAutowiredAnnotation(field);
- if (ann != null) {
- if (Modifier.isStatic(field.getModifiers())) {
- if (logger.isWarnEnabled()) {
- logger.warn("Autowired annotation is not supported on static fields: " + field);
- }
- return;
- }
- boolean required = determineRequiredStatus(ann);、
- // 将该字段封成AutowiredFieldElement对象,并放到缓存中
- currElements.add(new AutowiredFieldElement(field, required));
- }
- }
- });
-
- // 通过反射找出对应Class对象的所有Method
- ReflectionUtils.doWithLocalMethods(targetClass, new ReflectionUtils.MethodCallback() {
- @Override
- public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
- Method bridgedMethod = BridgeMethodResolver.findBridgedMethod(method);
- if (!BridgeMethodResolver.isVisibilityBridgeMethodPair(method, bridgedMethod)) {
- return;
- }
- // 通过反射找到该字段上所有的注解信息,并判断是否有@Autowired和@Value注解,如果有就将该字段封成AutowiredMethodElement对象
- AnnotationAttributes ann = findAutowiredAnnotation(bridgedMethod);
- if (ann != null && method.equals(ClassUtils.getMostSpecificMethod(method, clazz))) {
- if (Modifier.isStatic(method.getModifiers())) {
- if (logger.isWarnEnabled()) {
- logger.warn("Autowired annotation is not supported on static methods: " + method);
- }
- return;
- }
- if (method.getParameterTypes().length == 0) {
- if (logger.isWarnEnabled()) {
- logger.warn("Autowired annotation should only be used on methods with parameters: " +
- method);
- }
- }
- boolean required = determineRequiredStatus(ann);
- PropertyDescriptor pd = BeanUtils.findPropertyForMethod(bridgedMethod, clazz);
- // 将该字段封成AutowiredMethodElement对象
- currElements.add(new AutowiredMethodElement(method, required, pd));
- }
- }
- });
-
- elements.addAll(0, currElements);
- targetClass = targetClass.getSuperclass();
- }
- // 循环处理父类需要自动装配的元素
- while (targetClass != null && targetClass != Object.class);
- // 将需要自动装配的元素封装成InjectionMetadata对象,最后合并到Bean定义中
- return new InjectionMetadata(clazz, elements);
- }