DisposableBean,InitializingBean,ApplicationContextAware

?在spring容器初始化bean和銷毀bean的以前的操作有很多種,
  目前我知道的有:在xml中定義的時候用init-methoddestory-method,還有一種就是定義bean的時候?qū)崿F(xiàn)DisposableBeanInitializingBean 這兩個接口,打開InitializingBean的源碼:

public interface InitializingBean {

    /**
     * Invoked by a BeanFactory after it has set all bean properties supplied
     * (and satisfied BeanFactoryAware and ApplicationContextAware).
     * <p>This method allows the bean instance to perform initialization only
     * possible when all bean properties have been set and to throw an
     * exception in the event of misconfiguration.
     * @throws Exception in the event of misconfiguration (such
     * as failure to set an essential property) or if initialization fails.
     */
    void afterPropertiesSet() throws Exception;

}

根據(jù)注解很清楚的可以看出,afterPropertiesSet()表示在資源加載完以后,初始化bean之前執(zhí)行的方法,我猜想spring底層應(yīng)該會在初始化bean的時候,應(yīng)該會使用(bean instanceof InitializingBean)判斷是不是實現(xiàn)了這個接口,其實在很多框架中都是這么干的,但是因為沒研究過spring源碼,暫且還不知道底層原理。這樣我們就可以在初始化的時候,做一些自己想要做的事了。
  同理,DisposableBean就是在一個bean被銷毀的時候,spring容器會幫你自動執(zhí)行這個方法,估計底層原理也是差不多的,對于一些使用完之后需要釋放資源的bean,我們都會實現(xiàn)這個接口,或者是配置destory-method方法。源碼也基本是相似的,只是把afterPropertiesSet改為destroy。

ApplicationContextAware
  其實我們看到Aware就知道是干嘛用的了,就是屬性注入的,但是這個ApplicationContextAware的不同地方在于,實現(xiàn)了這個接口的bean,當(dāng)spring容器初始化的時候,會自動的將ApplicationContext注入進(jìn)來:

import org.apache.commons.lang.Validate;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
/**
 * applicationContext靜態(tài)化
 * 使用了ApplicationContextAware接口的類,如果受spring容器管理的
 * 話,那么就會自動的調(diào)用ApplicationContextAware中的setApplicationContext方法
 * @author Hotusm
 *
 */
@Service
@Lazy(false)
public class SpringContextHolder implements ApplicationContextAware,DisposableBean{
    
    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext)
            throws BeansException {
        
        SpringContextHolder.applicationContext=applicationContext;
    }
    //清空applicationContext 設(shè)置其為null
    @Override
    public void destroy() throws Exception {
        SpringContextHolder.clearHolder();
    }
    //獲得applicationContext
    public static ApplicationContext getApplicationContext() {
        //assertContextInjected();
        return applicationContext;
    }
    
    public static void clearHolder(){
        applicationContext=null;
    }
    //獲取Bean
    public static <T> T getBean(Class<T> requiredType){
        //assertContextInjected();
        return (T) getApplicationContext().getBean(requiredType);
    }
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name){
        assertContextInjected();
        return (T) getApplicationContext().getBean(name);
    }
    //判斷application是否為空
    public static void assertContextInjected(){
        Validate.isTrue(applicationContext==null, "application未注入 ,請在springContext.xml中注入SpringHolder!");
    }
    
}

因為我們在做開發(fā)的時候,并不是說在每一個地方都能將屬性注入到我們想要的地方去的,或者需要根據(jù)參數(shù)獲得不同的bean,比如在Utils使用到dao,我們就不能直接注入了,這個時候就是我們需要封裝springContext的時候了,而ApplicationContextAware就起了關(guān)鍵性的作用。

3:還有一種是注解的用法:

在指定方法上加上@PostConstruct@PreDestroy注解來制定該方法是在初始化之后還是銷毀之前調(diào)用。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,711評論 19 139
  • 這部分主要是開源Java EE框架方面的內(nèi)容,包括Hibernate、MyBatis、Spring、Spring ...
    雜貨鋪老板閱讀 1,577評論 0 2
  • 什么是Spring Spring是一個開源的Java EE開發(fā)框架。Spring框架的核心功能可以應(yīng)用在任何Jav...
    jemmm閱讀 16,785評論 1 133
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,290評論 6 342
  • 關(guān)于變量這一塊內(nèi)容,打一個比方,就像吃飯,量(內(nèi)容)就是飯,變量就是盛飯的碗,我們吃飯就是拿碗吃,我們要想使用一個...
    河許人閱讀 1,482評論 0 5

友情鏈接更多精彩內(nèi)容