springbean的生命周期(上)

本篇主要介绍springbean的生命周期第一部分,每一部分的分层是按照BeanPostProcessor后置处理器来完成的。

本篇完成内容为

1.将需要进行aop代理的对象存放到集合中(已存在对象AppConfig.class 通过BeanFactoryPostProcessor 完成CGLIB增强)

   BeanPostProcessor#AnnotationAwareAspectJAutoProxyCreator#postProcessBeforeInstantiation();

2.推断构造方法初始化对象(spring-ioc-推断构造函数-手动装配,spring-ioc-推断构造函数-自动装配)

   BeanPostProcessor#AutowiredAnnotationBeanPostProcessor#determineCandidateConstructors();

3.合并beanDefinition(后面补,前面BeanFactoryPostProcessor方法中已经做过一些合并处理)

4.处理循环依赖(spring-ioc-循环依赖)

   BeanPostProcessor#AnnotationAwareAspectJAutoProxyCreator#getEarlyBeanReference(); 

此篇内容先引入前篇文章的两张图

springbean的生命周期(上)_第1张图片

如图,此时bean后置处理器有6个,我们现在在测试环境的配置类中加上开启aop的支持

@EnableAspectJAutoProxy就会多出一个来AnnotationAwareAspectJAutoProxyCreator

老样子还是看一看BeanPostProcessor继承图实现类有很多......

 

springbean的生命周期(上)_第2张图片

个人觉得springbean的生命周期就是从创建bean开始的AbstractAutowireCapableBeanFactory#createBean()
所以我们有了 BeanPostProcessor 后置处理器的第一步解决了将需要进行代理的对象存到了一个集合中,下面来看代码

BeanPostProcessor#AnnotationAwareAspectJAutoProxyCreator#postProcessBeforeInstantiation();

Object bean = resolveBeforeInstantiation(beanName, mbdToUse);
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.
      // 判断是否为一个合成的bd(默认false) &&  默认true
      if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
         Class targetType = determineTargetType(beanName, mbd);
         if (targetType != null) {
            // 调用处理器 正常情况下恒定返回空,并记录代理对象
            bean = applyBeanPostProcessorsBeforeInstantiation(targetType, beanName);
            if (bean != null) {
               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;
         Object result = ibp.postProcessBeforeInstantiation(beanClass, beanName);
         if (result != null) {
            return result;
         }
      }
   }
   return null;
}
AnnotationAwareAspectJAutoProxyCreator
--postProcessBeforeInstantiation -- advisedBeans.put(object,false)--处理后返回null
advisedBeans 已经有appconfig 呢,bean工厂后置处理器已代理
public Object postProcessBeforeInstantiation(Class beanClass, String beanName) {
   Object cacheKey = getCacheKey(beanClass, beanName);

   if (!StringUtils.hasLength(beanName) || !this.targetSourcedBeans.contains(beanName)) {
      if (this.advisedBeans.containsKey(cacheKey)) {
         return null;
      }
      // 判断这个对象是否是一个代理对象,已经存在AppConfig.class
      if (isInfrastructureClass(beanClass) || shouldSkip(beanClass, beanName)) {
         this.advisedBeans.put(cacheKey, Boolean.FALSE);
         return null;
      }
   }

//有删减

}

protected boolean isInfrastructureClass(Class beanClass) {
   return (super.isInfrastructureClass(beanClass) ||
         (this.aspectJAdvisorFactory != null && this.aspectJAdvisorFactory.isAspect(beanClass)));
}
public boolean isAspect(Class clazz) {
   return (hasAspectAnnotation(clazz) && !compiledByAjc(clazz));
}

//返回true 包含注解@Aspect
private boolean hasAspectAnnotation(Class clazz) {
   return (AnnotationUtils.findAnnotation(clazz, Aspect.class) != null);
}
//返回false 获取到属性值 BServiceBo bServiceBo getName不包含AJC_MAGIC
private boolean compiledByAjc(Class clazz) {
   for (Field field : clazz.getDeclaredFields()) {
      if (field.getName().startsWith(AJC_MAGIC)) {
         return true;
      }
   }
   return false;
}

处理循环依赖(可参考 spring-ioc-循环依赖)

如果配置类没有加@EnableAspectJAutoProxy 注解 那么这个 后置处理器不会有,getEarlyBeanReference() 这段代码也直接返回空,如果加了 就会 在这个map中 earlyProxyReferences.put(cacheKey, bean)

   BeanPostProcessor#AnnotationAwareAspectJAutoProxyCreator#getEarlyBeanReference(); 

构造方法之所以不能解决循环依赖问题是因为后置处理器先处理了构造方法初始化才来解决循环依赖,顺序不同。

可参考重新绘制的流程图

springbean的生命周期(上)_第3张图片

 

你可能感兴趣的:(Spring,Framework,spring)