BeanPostProcessor 的接口定義,可以實現提供自己的實例化邏輯,依賴解析邏輯等,也可以以后在Spring容器實例化完畢,配置和初始化一個bean通過插入一個或多個的BeanPostProcessor實現一些自定義邏輯回調方法實現。
可以配置多個的BeanPostProcessor接口,控制這些的BeanPostProcessor接口,通過設置屬性順序執行順序提供的BeanPostProcessor實現了Ordered接口。
BeanPostProcessor可以對bean(或對象)操作實例,這意味著Spring IoC容器實例化一個bean實例,然后BeanPostProcessor的接口做好自己的工作。
ApplicationContext會自動檢測已定義實現的BeanPostProcessor接口和注冊這些bean類為后置處理器,可然后通過在容器創建bean,在適當時候調用任何bean。
示例:
下面的示例顯示如何編寫,注冊和使用BeanPostProcessor 可以在一個ApplicationContext 的上下文。
使用Eclipse IDE,然后按照下面的步驟來創建一個Spring應用程序:
這里是 HelloWorld.java 文件的內容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
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文件的內容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
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方法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
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文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
<? 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配置文件完成后,讓我們運行應用程序。如果一切順利,這將打印以下信息:
1
2
3
4
5
|
BeforeInitialization : helloWorld Bean is going through init. AfterInitialization : helloWorld Your Message : Hello World! Bean will destroy now. |