BeanPostProcessor
BeanPostProcessor接口作用是:如果我們需要在Spring容器完成Bean的實例化、配置和其他的初始化前後添加一些自己的邏輯處理,我們就可以定義一個或者多個BeanPostProcessor接口的實現,然後註冊到容器中。
Spring中Bean(非實現beanpostprocessor的類)的實例化過程圖示:
由上圖可以看到,Spring中的BeanPostProcessor在實例化過程處於的位置,BeanPostProcessor接口有兩個方法需要實現:postProcessBeforeInitialization和postProcessAfterInitialization,
一、簡單介紹
1、init-method方法,初始化bean的時候執行,可以針對某個具體的bean進行配置。init-method需要在applicationContext.xml配置文檔中bean的定義裏頭寫明。例如:<bean id="TestBean" class="nju.software.xkxt.util.TestBean" init-method="init"></bean>
這樣,當TestBean在初始化的時候會執行TestBean中定義的init方法。
2、afterPropertiesSet方法,初始化bean的時候執行,可以針對某個具體的bean進行配置。afterPropertiesSet 必須實現 InitializingBean接口。實現 InitializingBean接口必須實現afterPropertiesSet方法。
3、BeanPostProcessor,針對所有Spring上下文中所有的bean,可以在配置文檔applicationContext.xml中配置一個BeanPostProcessor,然後對所有的bean進行一個初始化之前和之後的代理。BeanPostProcessor接口中有兩個方法: postProcessBeforeInitialization和postProcessAfterInitialization。 postProcessBeforeInitialization方法在bean初始化之前執行, postProcessAfterInitialization方法在bean初始化之後執行。
總之,afterPropertiesSet 和init-method之間的執行順序是afterPropertiesSet 先執行,init-method 後執行。從BeanPostProcessor的作用,可以看出最先執行的是postProcessBeforeInitialization,然後是afterPropertiesSet,然後是init-method,然後是postProcessAfterInitialization。
二、相關用法及代碼測試
1、PostProcessor類,實現BeanPostProcessor接口,實現接口中的postProcessBeforeInitialization,postProcessAfterInitialization方法
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
/**
* 定義Bean初始化前後的動作
*
*
*/
public class PostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
System.out.println("------------------------------");
System.out.println("對象" + beanName + "開始實例化");
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
System.out.println("對象" + beanName + "實例化完成");
System.out.println("------------------------------");
return bean;
}
}
該PostProcessor類要作爲bean定義到applicationContext.xml中,如下
自己的名字
<bean class="XXX.util.PostProcessor"></bean>
2、TestBean類,用做測試Bean,觀察該Bean初始化過程中上面4個方法執行的先後順序和內容。實現InitializingBean接口,並且實現接口中的afterPropertiesSet方法。最後定義作爲init-method的init方法。
import org.springframework.beans.factory.InitializingBean;
/**
* 用做測試Bean,觀察該Bean初始化過程中上面4個方法執行的先後順序和內容
*
*
*/
public class TestBean implements InitializingBean {
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void init() {
System.out.println("init-method is called");
System.out.println("******************************");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("******************************");
System.out.println("afterPropertiesSet is called");
System.out.println("******************************");
}
}
啓動Tomcat服務器,可以看到服務器啓動過程中,完成對Bean進行初始化。執行結果如下:
------------------------------
對象TestBean開始實例化
******************************
afterPropertiesSet is called
******************************
init-method is called
******************************
對象TestBean實例化完成
------------------------------