Bean初始化之postProcessBeforeInitialization、afterPropertiesSet、init-method、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方法

[java] view plaincopy

package nju.software.xkxt.util;  

  

import org.springframework.beans.BeansException;  

import org.springframework.beans.factory.config.BeanPostProcessor;  

  

/** 

 * 定義Bean初始化前后的動作 

 *  

 * @author typ 

 *  

 */  

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="nju.software.xkxt.util.PostProcessor"></bean>

 

2、TestBean類,用做測試Bean,觀察該Bean初始化過程中上面4個方法執行的先后順序和內容。實現InitializingBean接口,並且實現接口中的afterPropertiesSet方法。最后定義作為init-method的init方法。

 

package nju.software.xkxt.util;  
   
import org.springframework.beans.factory.InitializingBean;  
   
/** 
 * 用做測試Bean,觀察該Bean初始化過程中上面4個方法執行的先后順序和內容 
 *  
 * @author typ 
 *  
 */ 
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實例化完成

------------------------------

 

轉自:寫代碼簡單比較init-method,afterPropertiesSet和BeanPostProcessor - 綜合編程類其他綜合 - 紅黑聯盟

https://www.2cto.com/kf/201310/251778.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM