[Spring] annotation-driven development - life cycle - BeanPostProcessor of use and principles

This blog demo source address
https://github.com/suchahaerkang/spring-annotation.git

Use of 1 BeanPostProcessor

BeanPostProcessor spring is provided in an interface, the interface provides two default methods, respectively before and after some processing initialize all the components
Here Insert Picture Description
Look at the source code, the timing analysis of the implementation of these two methods
Here Insert Picture Description
Here Insert Picture Description
Here we use the code to test it, first of all we have a custom component implements the interface BeanPostProcessor

/**
 * @description:
 * @author: sukang
 * @date: 2020-03-06 12:27
 */
@Component
public class MyBeanPostProcessor implements BeanPostProcessor {

    //组件创建并属性赋值之后,初始化函数执行之前执行这个方法
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("postProcessBeforeInitialization方法执行...");
        return bean;
    }

    //组件创建并属性赋值之后,初始化函数执行之后执行这个方法
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println("postProcessAfterInitialization方法执行...");
        return bean;
    }

}

Write test cases

 //测试BeanPostProcessor
 @Test
  public void test3(){
      //创建容器
      System.out.println("IOC容器创建...");
      ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfLifeCycle.class);
  }

operation result
Here Insert Picture Description

Principle 2 BeanPostProcessor of

Here we look at the source code to analyze the principle of BeanPostProcessor
by looking at the source code to analyze Dog component creation, assignment and initialization
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
source code has been reading, pseudo-code simply summarize the principles BeanPostProcessor

 //组件创建之后,对组件进行属性赋值,
 populateBean()
 initializeBean(){
      //遍历容器中的所有BeanPostProcessor,调用他们的postProcessBeforeInitialization()方法
      applyBeanPostProcessorsBeforeInitialization()
      //初始化组件
      invokeInitMethods()
      //遍历容器中的所有BeanPostProcessor,调用他们的postProcessAfterInitialization()方法
      applyBeanPostProcessorsAfterInitialization()
  }

3 spring provides a number of components to achieve BeanPostProcessor

Here Insert Picture Description
Here's a brief look at the ApplicationContextAwareProcessor this component
we realize setApplicationContext () method ApplicationContextAware interface components Dog basis of the above, this method will be called before the component initialization Dog and Dog ApplicationContext container as a parameter to bring the components, the components may Dog use the ApplicationContext container.
Here Insert Picture Description
Container has been able to bring it before the component initialization is achieved because the effect ApplicationContextAwareProcessor this component, we look at this source components
Here Insert Picture Description
Here Insert Picture Description

Four pairs of the bean life cycle summary

Published 78 original articles · won praise 32 · views 90000 +

Guess you like

Origin blog.csdn.net/suchahaerkang/article/details/104693346