Spring source code-the fifth post processor notes

In the blog of manually injecting source code , it records the manually injected source code, but it is actually the use of the sixth post processor; so this blog mainly records the learning of the
fifth post processor; A post processor is actually used in conjunction with the sixth post processor during manual injection

org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#populateBean

The fifth and sixth post-processors are all called in this method. The fifth post-processor is used to determine whether attribute injection is required. This is also an extension point. If we want to To extend the attribute injection of a certain bean, that is to say, we should perform attribute injection by ourselves, instead of using spring attribute injection, then we can implement a post processor and implement the fifth post processor method. In this method, return false for the specified bean, so that the specified bean will not call the spring attribute injection method during the initialization process

Next, we first look at the source code

/**
 * 根据该标识判断是否需要进行属性注入
 */
boolean continueWithPropertyPopulation = true;

/**
 * 第五次调用后置处理器  判断是否需要填充属性;返回false,表示无需进行属性注入
 * 是调用的postProcessorAfterInstantiation方法,在这个方法里面,如果返回false,就不会再进行属性注入;
 * 所以,如果程序员要对所有注入的bean都不进行属性注入,就自己实现该方法,返回false即可(但是这种需求,一般也很少见吧)
 * 但是如果,只是要某一个bean不需要spring帮我们完成属性注入,一定要在扩展方法中加上对beanName的判断,否则的话,会导致所有的bean不进行属性注入
 * 因为每个bean在初始化的时候,都会去调用我们自己扩展的方法,只要有一个后置处理器的方法返回false,就不会属性填充了
 */
if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
    
    
  for (BeanPostProcessor bp : getBeanPostProcessors()) {
    
    
    if (bp instanceof InstantiationAwareBeanPostProcessor) {
    
    
      InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
      if (!ibp.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName)) {
    
    
        continueWithPropertyPopulation = false;
        break;
      }
    }
  }
}

if (!continueWithPropertyPopulation) {
    
    
  return;
}

This is the core method of the fifth post processor. When the ibp.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName) method returns false, the continueWithPropertyPopulation variable will be set to false, and then it will return directly. Will continue to perform property injection

In the post processor provided by spring, there is not too much logic processing for this method, and the direct default is to return true, that is, the sixth post processor of spring is used to complete the attribute filling

There is only one point that needs special attention: if we need to perform special processing on a bean, that is, do not use spring's attribute injection logic, then we need to extend the fifth post processor, such as: we are now I have implemented a beanPostProcessor implementation class, MySelfBeanPostProcessor. In the fifth post processor, we must determine whether the beanName is specially processed by us. If it is, return false, because each bean is initialized, It will execute all post processors and the corresponding judgment methods. As long as one beanPostProcessor returns false, no property injection will be performed

Guess you like

Origin blog.csdn.net/CPLASF_/article/details/111645699