Spring___BeanPostProcessor Interface (post processor)

One,

BeanPostProcessor接口(后置处理器)
Bean对象在实例化和依赖注入完毕后,在初始化方法的前后
注意是Bean实例化完毕后及依赖注入完成后触发的
可以根据业务需求在相关方法里添加逻辑

The interface has two methods:
instantiated dependency injection is completed and before executing the initialization method: postProcessBeforeInitialization
postProcessAfterInitialization: instantiated dependency injection is completed and, after completion of execution of the initialization method

Second, analyzed by demo

This is a pojo type, StudentA realized InitializingBean and DisposableBean also notes @ PostConstruct, @ PreDestroy well as custom methods; specifically to see my blog

package com.pojo;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
public class StudentA implements InitializingBean,DisposableBean{
    private String name;
    private String stuId;
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
        System.out.println("设值注入Name:"+name);
    }
    
    public String getStuId() {
        return stuId;
    }
    
    public void setStuId(String stuId) {
        this.stuId = stuId;
        System.out.println("设值注入stuId:"+stuId);
    }
    
    public StudentA(String name, String stuId) {
        super();
        this.name = name;
        this.stuId = stuId;
    }
    
    public StudentA() {
        super();
        // TODO Auto-generated constructor stub
        System.out.println("通过无参构造...StudentA实例化");
    }
    
    @Override
    public String toString() {
        return "StudentA [name=" + name + ", stuId=" + stuId + "]";
    }
    
    /**
     * 重写DisposableBean接口里的方法
     * 在销毁前回调
     */
    @Override
    public void destroy() throws Exception {
        // TODO Auto-generated method stub
        System.out.println("DisposableBean接口的destroy()");
    }
    
    /**
     * 重写InitializingBean接口里的方法
     * 在实例化并设值注入后初始化回调
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        // TODO Auto-generated method stub
        System.out.println("InitializingBean接口的afterPropertiesSet()");
    }
    
    /**
     * 自定义初始化后回调
     * 对应着bean里的init-method="start"
     */
    public void start() {
        // TODO Auto-generated method stub
        System.err.println("自定义初始化回调...init-method=\"start\"");
    }
    
    /**
     * 自定义销毁前回调
     * 对应着bean里的destroy-method="end"
     */
    public void end() {
        // TODO Auto-generated method stub
        System.out.println("自定义销毁回调...destroy-method=\"end\"");
    }
    
    /**
     * 通过@PreDestroy注解
     * 初始化后回调方法
     */
    @PostConstruct
    public void postConstruct(){
        System.out.println("初始化后回调方法...@postConstruct注解");
    }
    
    
    /**
     * 通过@PreDestroy注解
     * 销毁前回调方法
     */
    @PreDestroy
    public  void preDestory(){
        System.out.println("销毁前回调方法...@preDestory注解");
    }

    
    
}

StuBeanPostProcessor realized BeanPostProcessor interface, rewrote its two methods

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

public class StuBeanPostProcessor implements BeanPostProcessor {
    /**
     * 实例化、依赖注入完毕,
     * 在调用显示的初始化之前完成一些定制的初始化任务
     */
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        // TODO Auto-generated method stub
        System.out.println("postProcessBeforeInitialization 实例化、依赖注入完毕,初始化之前执行");
        System.out.println("postProcessBeforeInitialization中bean:"+bean+",beanName:"+beanName);
        return bean;
    }

    /**
     * 实例化、依赖注入、初始化完毕时执行
     */
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        // TODO Auto-generated method stub
        System.out.println("postProcessAfterInitialization初始化完毕时执行");
        System.out.println("postProcessAfterInitialization中bean:"+bean+",beanName:"+beanName);
        return bean;
    }
}

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
        
        
        <!-- 如果想使用@ Resource 、@ PostConstruct、@ PreDestroy等注解就必须声明CommonAnnotationBeanPostProcessor -->
        <context:annotation-config/> 
        
        <!-- 注册studentA对象 -->
        <bean class="com.pojo.StudentA" id="studentA" init-method="start" destroy-method="end">
            <!-- 设值注入 -->
            <property name="name" value="zsl"/>
            <property name="stuId" value="zsl33"/>
        </bean> 
        
        <!-- 注册StuBeanPostProcessor,它继承了BeanPostProcessor -->
        <bean class="com.pojo.StuBeanPostProcessor"/>
</beans>

test:

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.pojo.Dog;
import com.pojo.StudentA;
import org.springframework.beans.factory.BeanNameAware;
public class Test {
    @SuppressWarnings("resource")//去警告,问题不大不要慌
    public static void main(String[] args) {
        ClassPathXmlApplicationContext applicationContext = 
                new ClassPathXmlApplicationContext("applicationContext.xml");
        
        StudentA bean = (StudentA) applicationContext.getBean("studentA");
System.out.println(bean);
        
        /**
         * 在非web环境下需要手动关闭IOC容器,则需调用registerShutdownHook()方法
         * 而web环境下已有相应的配置进行关闭IOC容器
         */
        applicationContext.registerShutdownHook();
    }
}

result:

Third, the analysis
can be seen from the above Demo:
execution order:

Initialization:
Constructor Construct -> Properties injection -> postProcessBeforeInitialization Interface
-> @ PostConstruct -> InitializingBean Interface -> bean init-method from the method defined -> postProcessAfterInitialization Interface

When destroying: @PreDestroy -> DisposableBean Interface -> method of destoryMethod custom bean

Highlighted once again:

BeanPostProcessor接口(后置处理器)
Bean对象在实例化和依赖注入完毕后,在初始化方法的前后
注意是Bean实例化完毕后及依赖注入完成后触发的

Guess you like

Origin www.cnblogs.com/zhangsonglin/p/10939740.html