Spring的bean後處理器和容器後處理器
阿新 • • 發佈:2019-02-12
Spring後處理器的作用
Bean後處理器:即當Spring容器例項化Bean例項之後進行的增強處理。
容器後處理器:對容器本身進行處理,並總是在容器例項化其他任何Bean之前讀取配置檔案的元資料並可能修改這些資料。
通過使用後處理器,可以模擬Spring的AOP切面程式設計
bean後處理器:BeanPostProcessor
BeanPostProcessor 介面定義回撥方法,你可以實現該方法來提供自己的例項化邏輯,依賴解析邏輯等。你也可以在 Spring 容器通過插入一個或多個 BeanPostProcessor 的實現來完成例項化,配置和初始化一個bean之後實現一些自定義邏輯回撥方法。
有時希望在spring IoC容器初始化受管Bean前、屬性設定後對該Bean先做一些預處理,或者在容器銷燬受管Bean之前自己釋放資源。Spring IoC提供了BeanPostProcessor介面用於實現該操作
若這個介面的某個實現類被註冊到某個容器,那麼該容器的每個受管Bean在呼叫初始化方法前,都會獲得該介面實現類的一個回撥。容器呼叫介面定義的方法時會將該受管Bean的例項和名字通過引數傳入方法,經過處理後通過方法的返回值返回給容器。根據這個原理就可以很輕鬆的自定義受管Bean
BeanPostProcessor後處理器的使用
要使用BeanPostProcessor回撥,就必須先在容器中註冊實現該介面的類。BeanFactory和ApplicationContext容器的註冊方式是不一樣的:若使用BeanFactory,則必須要顯示的呼叫其addBeanPostProcessor()方法進行註冊。若使用ApplicationContext,那麼容器會在配置檔案在中自動尋找實現了BeanPostProcessor介面的Bean,然後自動註冊。
後處理器的使用例項
//bean
public class HelloPost {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public void init(){
System.out.println("呼叫自身的init方法");
}
public void destroy(){
System.out.println("呼叫自身的destroy方法");
}
public void show(){
System.out.println("hello world->"+message);
}
}
//bean後處理器
public class HelloHander implements BeanPostProcessor,Ordered {
@Override
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
System.out.println("正在呼叫postProcessAfterInit->"+beanName);
return bean;
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
System.out.println("正在呼叫postProcessBeforeInit->"+beanName);
return bean;
}
@Override
public int getOrder() {
// TODO Auto-generated method stub
return 1;
}
}
//配置檔案
<beans ...>
<bean id="helloPost" class="org.spring.PostBean.HelloPost" init-method="init" destroy-method="destroy">
<property name="message" value="測試後處理器" />
</bean>
<!-- 後處理器的配置 -->
<bean class="org.spring.PostBean.HelloHander" />
</beans>
//測試主程式
public class TestMain {
public static void main(String[] args){
AbstractApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
HelloPost post = context.getBean("helloPost",HelloPost.class);
System.out.println("post.message->"+post.getMessage());
//關閉hook的方法,必須註冊為AbstractApplicationContext才可以呼叫
context.registerShutdownHook();
}
}
//輸出結果:
正在呼叫postProcessBeforeInit->helloPost
呼叫自身的init方法
正在呼叫postProcessAfterInit->helloPost
post.message->測試後處理器
//呼叫registerShutdownHook()工作中
呼叫自身的destroy方法
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
通過結果可以看出:後處理器->bean自身定義的方法->bean呼叫的方法
多個後處理器的結合使用
當需要定義多個後處理器時,在xml配置所有的後處理器之後,還需要指定他們的執行順序
通過實現Ordered介面並重寫getOrder()屬性就可以來指定它們的執行順序了。order的值越小,越先執行
容器後處理器
容器後處理器實現的是BeanFactoryPostProcessor介面,其餘的和bean後處理器的相關類似
容器後處理器使用例項
//容器後處理器
public class FactoryPost implements BeanFactoryPostProcessor {
@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory arg0)
throws BeansException {
System.out.println("對容器進行處理");
}
}
//xml配置
<!-- 容器後處理器的配置 -->
<bean class="org.spring.PostFactory.FactoryPost" />
//測試主程式
public class TestMain {
public static void main(String[] args){
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24