转自:
BeanPostProcessor(Spring后置处理器)如何使用呢?
BeanPostProcessor接口:
我们通常称其为后置处理器,通过重写接口中的方法可实现初始化前后的操作
BeanPostProcessor 接口源码
public interface BeanPostProcessor {
Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException;
Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException;
}
postProcessBeforeInitialization:当Bean实例化、依赖注入后、初始化前调用
postProcessAfterInitialization :当Bean实例化、依赖注入、初始化都完成后调用
注意事项: 当我们需添加多个后置处理器实现类时,
默认情况下 Spring 容器会根据后置处理器的定义顺序来依次调用,
当然我们可实现Ordered接口中的getOrder方法设置后置处理器的执行属性,
也可以通过实现 Ordered 接口的 getOrder 方法指定后置处理器的执行顺序
该方法返回值为整数,默认值为 0,值越大优先级越低。
例 下面使用 Eclipse IDE 演示 BeanPostProcessor 接口的用法,
步骤 : 创建 SpringDemo 项目,
并在 src 目录下创建com.java265包 添加相应的 jar 包,可参阅第一个Spring程序
在com.java265包下创建
HelloWorld、InitHelloWorld、InitHelloWorld2 和 MainApp 类。
在 src 目录下创建 Spring 配置文件 Beans.xml。
运行SpringDemo项目 HelloWorld 类代码
package com.java265;
public class HelloWorld {
private String message;
public void setMessage(String message) {
this.message = message;
}
public void getMessage() {
System.out.println("Message : " + message);
}
public void init() {
System.out.println("Bean正在初始化");
}
public void destroy() {
System.out.println("Bean将要被销毁");
}
}
InitHelloWorld 类代码
package com.java265;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.Ordered;
public class InitHelloWorld implements BeanPostProcessor, Ordered {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("A Before : " + beanName);
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("A After : " + beanName);
return bean;
}
@Override
public int getOrder() {
return 8;
}
}
注意事项: postProcessBeforeInitialization 和 postProcessAfterInitialization 方法返回值不能为 null,
否则会报空指针异常或者通过 getBean() 方法 获取不到 Bean 实例对象,
因为后置处理器从Spring IoC 容器中取出 Bean 实例对象后没有再次放回到 IoC 容器中
InitHelloWorld2 类代码
package com.java265;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.Ordered;
public class InitHelloWorld2 implements BeanPostProcessor, Ordered {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("B Before : " + beanName);
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("B After : " + beanName);
return bean;
}
@Override
public int getOrder() {
return 0;
}
}
Beans.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" xsi:schemalocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean class="com.java265.HelloWorld" destroy-method="destroy" id="helloWorld" init-method="init">
<property name="message" value="Hello World!">
</property></bean>
<!-- 注册处理器 -->
<bean class="com.java265.InitHelloWorld"></bean>
<bean class="com.java265.InitHelloWorld2"></bean>
</beans>
MainApp 类
package com.java265;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
AbstractApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
obj.getMessage();
context.registerShutdownHook();
}
}
-----运行输出结果-----
B Before : helloWorld
A Before : helloWorld
Bean正在初始化
B After : helloWorld
A After : helloWorld
Message : Hello World!
Bean将要被销毁
从以上的运行代码中,我们可以得到
postProcessBeforeInitialization 方法是在 Bean 实例化和依赖注入后,自定义初始化方法前执行的
postProcessAfterInitialization 方法是在自定义初始化方法后执行的
由于 getOrder 方法返回值越大,优先级越低,所以 InitHelloWorld2 先执行