关于springboot:Spring-Boot-如何处理Autowired

咱们还以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!

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理