Spring發布處理器
BeanPostProcessor 的接口定義,可以實現提供自己的實例化邏輯,依賴解析邏輯等,也可以以後在Spring容器實例化完畢,配置和初始化一個bean通過插入一個或多個的BeanPostProcessor實現一些自定義邏輯回調方法實現。
可以配置多個的BeanPostProcessor接口,控製這些的BeanPostProcessor接口,通過設置屬性順序執行順序提供的BeanPostProcessor實現了Ordered接口。
BeanPostProcessor可以對bean(或對象)操作實例,這意味著Spring IoC容器實例化一個bean實例,然後BeanPostProcessor的接口做好自己的工作。
ApplicationContext會自動檢測已定義實現的BeanPostProcessor接口和注冊這些bean類為後置處理器,可然後通過在容器創建bean,在適當時候調用任何bean。
示例:
下麵的示例顯示如何編寫,注冊和使用BeanPostProcessor 可以在一個ApplicationContext 的上下文。
使用Eclipse IDE,然後按照下麵的步驟來創建一個Spring應用程序:
步驟 | 描述 |
---|---|
1 | Create a project with a name SpringExample and create a package com.yiibai under the src folder in the created project. |
2 | Add required Spring libraries using Add External JARs option as explained in the Spring Hello World Example chapter. |
3 | Create Java classes HelloWorld, InitHelloWorld and MainApp under the com.yiibaipackage. |
4 | Create Beans configuration file Beans.xml under the src folder. |
5 | The final step is to create the content of all the Java files and Bean Configuration file and run the application as explained below. |
這裡是 HelloWorld.java 文件的內容:
package com.yiibai; public class HelloWorld { private String message; public void setMessage(String message){ this.message = message; } public void getMessage(){ System.out.println("Your Message : " + message); } public void init(){ System.out.println("Bean is going through init."); } public void destroy(){ System.out.println("Bean will destroy now."); } }
這是實現BeanPostProcessor,之前和之後的任何bean的初始化它打印一個bean的名字非常簡單的例子。可以因為有兩個後處理器的方法對內部bean對象訪問之前和實例化一個bean後執行更複雜的邏輯。
這裡是InitHelloWorld.java文件的內容:
package com.yiibai; import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.beans.BeansException; public class InitHelloWorld implements BeanPostProcessor { public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { System.out.println("BeforeInitialization : " + beanName); return bean; // you can return any other object as well } public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { System.out.println("AfterInitialization : " + beanName); return bean; // you can return any other object as well } }
以下是MainApp.java 文件的內容。在這裡,需要注冊一個關閉掛鉤registerShutdownHook() 是在AbstractApplicationContext類中聲明的方法。這將確保正常關閉,並調用相關的destroy方法。
package com.yiibai; 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(); } }
下麵是init和destroy方法需要的配置文件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 id="helloWorld" class="com.yiibai.HelloWorld" init-method="init" destroy-method="destroy"> <property name="message" value="Hello World!"/> </bean> <bean class="com.yiibai.InitHelloWorld" /> </beans>
創建源代碼和bean配置文件完成後,讓我們運行應用程序。如果一切順利,這將打印以下信息:
BeforeInitialization : helloWorld Bean is going through init. AfterInitialization : helloWorld Your Message : Hello World! Bean will destroy now.