一、作用:spring容器在管理其下bean时,一般有创建、初始化和销毁的生命周期,并允许用户自定义初始化和销毁方法

单例bean容器启动时bean后会执行初始化方法,关闭容器执行销毁方法

多例bean容器启动时不会执行初始化方法,使用时才会初始化,关闭容器不会执行销毁方法

二、自定义初始化销毁方法

方式1:@Bean注解属性initMethod,destroyMethod

@Configuration
public class ConfigurationBean {
    @Bean(value="person", initMethod="init", destroyMethod="destroy") 
    public Person getPerson() {
        Person person = new Person();
        person.setAge(25);
        person.setName("小东");
        return person;
    }
}
public class Person {
    private int age;
    private String name;
    
    public Person() {
        super();
        System.out.println("person constrct....");
    }
    
    public void init() {
        System.out.println("person init .....");
    }
    
    public void destroy() {
        System.out.println("person destroy .....");
    }
    
    public int getAge() {
        return age;
    }
.....

 方式2:实现InitializingBean、DisposableBean接口

public class Person implements InitializingBean, DisposableBean{
    private int age;
    private String name;
    
    public Person() {
        super();
        System.out.println("person constrct....");
    }
    
    @Override
    public void destroy() throws Exception {
        System.out.println("person destroy....");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("person afterPropertiesSet...");
    }
....

控制台打印:

person constrct....
person afterPropertiesSet...
person destroy....

方式3:@PostConstruct和@PreDestroy

public class Person {
    private int age;
    private String name;
    
    public Person() {
        super();
        System.out.println("person constrct....");
    }
    
    @PostConstruct
    public void init() {
        System.out.println("person init....");
    }
    
    @PreDestroy
    public void destroy() {
        System.out.println("person destroy....");
    }
  ....

三、BeanPostProcessor接口

postProcessBeforeInitialization()和postProcessAfterInitialization()方法分别用于容器中所有的bean初始化(上述几种初始化方式)之前和初始化之后调用

public class MyBeanPostProcessor implements BeanPostProcessor{
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName)
            throws BeansException {
        System.out.println("postProcessBeforeInitialization:" + beanName +"," + bean);
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName)
            throws BeansException {
        System.out.println("postProcessAfterInitialization:" + beanName +"," + bean);
        return bean;
    }
}
@Configuration
public class ConfigurationBean {
    @Bean(value="person") 
    public Person getPerson() {
        Person person = new Person();
        person.setAge(25);
        person.setName("小东");
        return person;
    }
    
    @Bean
    public MyBeanPostProcessor getMyBeanPostProcessor() {
        return new MyBeanPostProcessor();
    }
}

控制台打印

person constrct....
postProcessBeforeInitialization:person,this.name=小东,this.age=25
person init....
postProcessAfterInitialization:person,this.name=小东,this.age=25
person destroy....

四、BeanPostProcessor源码解析:

populateBean(beanName, mbd, instanceWrapper);   //bean 赋值操作
if (exposedObject != null) {    //初始化操作
wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);   //初始化之前
invokeInitMethods(beanName, wrappedBean, mbd);  //初始化
wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);   //初始化之后
}

 五、BeanPostProcessor spring底层应用

1、ApplicationContextAwareProcessor implements BeanPostProcessor  用户实现ApplicationContextAware接口即可复写setApplicationContext方法拿到上下问对象

2、赋值,生命周期,@Autowired等功能都是用BeanPostProcessor 完成 。。。。