The life cycle of springbean (part 1)

This article mainly introduces the first part of springbean's life cycle. The layering of each part is completed in accordance with the BeanPostProcessor post processor.

The completed content of this article is

1. Store the objects that need to be aop proxy in the collection (the existing object AppConfig.class completes CGLIB enhancement through BeanFactoryPostProcessor)

   BeanPostProcessor#AnnotationAwareAspectJAutoProxyCreator#postProcessBeforeInstantiation();

2. Inferred construction method to initialize the object ( spring-ioc-inferred constructor-manual assembly , spring-ioc-inferred constructor-automatic assembly )

   BeanPostProcessor#AutowiredAnnotationBeanPostProcessor#determineCandidateConstructors();

3. Merging beanDefinition (supplemented later, some merge processing has been done in the previous BeanFactoryPostProcessor method)

4. Handling circular dependencies ( spring-ioc-cyclic dependencies )

   BeanPostProcessor#AnnotationAwareAspectJAutoProxyCreator#getEarlyBeanReference(); 

This article first introduces the two pictures of the previous article

As shown in the figure, there are 6 bean post processors at this time. We now add the support of enabling aop to the configuration class of the test environment

@EnableAspectJAutoProxy will have one more AnnotationAwareAspectJAutoProxyCreator

As always, take a look at the BeanPostProcessor inheritance diagram and there are many implementation classes...

 

个人觉得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;
      }
   }

//There are cuts

}

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;
}

Handling circular dependencies (refer to  spring-ioc-cyclic dependencies )

If the configuration class is not annotated with @EnableAspectJAutoProxy, then this post processor will not be present. The code of getEarlyBeanReference() will also return empty directly. If it is added, it will be in the map  earlyProxyReferences.put(cacheKey, bean)

   BeanPostProcessor#AnnotationAwareAspectJAutoProxyCreator#getEarlyBeanReference(); 

The reason why the construction method cannot solve the circular dependency problem is that the post-processor first processes the initialization of the construction method before solving the circular dependency, and the order is different.

Refer to the redrawn flow chart

 

Guess you like

Origin blog.csdn.net/qq_38108719/article/details/103385637