以下内容引用自http://wiki.jikexueyuan.com/project/spring/bean-post-processors.html:
Bean后置处理器
BeanPostProcessor接口定义回调方法,你可以实现该方法来提供自己的实例化逻辑,依赖解析逻辑等。你也可以在Spring容器通过插入一个或多个BeanPostProcessor的实现来完成实例化,配置和初始化一个bean之后实现一些自定义逻辑回调方法。
你可以配置多个BeanPostProcessor接口,通过设置BeanPostProcessor实现的Ordered接口提供的order属性来控制这些BeanPostProcessor接口的执行顺序。
BeanPostProcessor可以对bean(或对象)实例进行操作,这意味着Spring IoC容器实例化一个bean实例,然后BeanPostProcessor接口进行它们的工作。
ApplicationContext会自动检测由BeanPostProcessor接口的实现定义的bean,注册这些bean为后置处理器,然后通过在容器中创建bean,在适当的时候调用它。
例子:
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.jsoft.testspring</groupId> <artifactId>testbeanpostprocessor</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>testbeanpostprocessor</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- Spring Core --> <!-- http://mvnrepository.com/artifact/org.springframework/spring-core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.1.4.RELEASE</version> </dependency> <!-- Spring Context --> <!-- http://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.4.RELEASE</version> </dependency> </dependencies> </project>
HelloWorld.java:
package com.jsoft.testspring.testbeanpostprocessor; public class HelloWorld { private String messageString; public void setMessage(String message){ this.messageString = message; } public void getMessage(){ System.out.println(this.messageString); } public void initCustom(){ System.out.println("初始化"); } public void destroyCustom(){ System.out.println("销毁"); } }
InitHelloWorld.java:
package com.jsoft.testspring.testbeanpostprocessor; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; public class InitHelloWorld implements BeanPostProcessor { public Object postProcessAfterInitialization(Object arg0, String arg1) throws BeansException { // TODO Auto-generated method stub System.out.println("After HelloWorld"); return arg0; } public Object postProcessBeforeInitialization(Object arg0, String arg1) throws BeansException { // TODO Auto-generated method stub System.out.println("Before HelloWorld"); return arg0; } }
这是实现BeanPostProcessor非常简单的例子,它在任何bean的初始化的之前和之后输入该bean的名称。你可以在初始化bean的之前和之后实现更复杂的逻辑,因为你有两个访问内置bean对象的后置处理程序的方法。
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.xsd"> <bean id="helloWorld" class="com.jsoft.testspring.testbeanpostprocessor.HelloWorld" init-method="initCustom" destroy-method="destroyCustom"> <property name="Message" value="Hello World!"></property> </bean> <bean class="com.jsoft.testspring.testbeanpostprocessor.InitHelloWorld"></bean> </beans>
App.java:
package com.jsoft.testspring.testbeanpostprocessor; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** * Hello world! * */ public class App { public static void main( String[] args ) { AbstractApplicationContext abstractApplicationContext = new ClassPathXmlApplicationContext("beans.xml"); HelloWorld helloWorld = (HelloWorld)abstractApplicationContext.getBean("helloWorld"); helloWorld.getMessage(); abstractApplicationContext.registerShutdownHook(); } }
在这里,你需要注册一个在AbstractApplicationContext类中声明的关闭hook的registerShutdownHook()方法。它将确保正常关闭,并且调用相关的destroy方法。
测试结果:
总结:
InitHelloWorld类集成自BeanPostProcessor,然后在bean中指定class为InitHelloWorld即可,这里分开写主要是为了展示两个方法的效果,当然这个类的两个方法可以直接写在HelloWorld中,直接在HelloWorld类中集成BeanPostProcessor,然后去实现两个方法。
测试工程:https://github.com/easonjim/5_java_example/tree/master/springtest/test6/testbeanpostprocessor