国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設(shè)計 > 正文

spring中的DisposableBean和InitializingBean,ApplicationContextAware的用法

2019-11-14 15:10:24
字體:
供稿:網(wǎng)友

在sPRing容器初始化bean和銷毀bean的以前的操作有很多種,
  目前我知道的有:在xml中定義的時候用init-method和destory-method,還有一種就是定義bean的時候?qū)崿F(xiàn)DisposableBean和InitializingBean 這兩個接口,打開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底層應該會在初始化bean的時候,應該會使用(bean instanceof InitializingBean)判斷是不是實現(xiàn)了這個接口,其實在很多框架中都是這么干的,但是因為沒研究過spring源碼,暫且還不知道底層原理。這樣我們就可以在初始化的時候,做一些自己想要做的事了。
  同理,DisposableBean就是在一個bean被銷毀的時候,spring容器會幫你自動執(zhí)行這個方法,估計底層原理也是差不多的,對于一些使用完之后需要釋放資源的bean,我們都會實現(xiàn)這個接口,或者是配置destory-method方法。源碼也基本是相似的,只是把afterPropertiesSet改為destroy。


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

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ā)的時候,并不是說在每一個地方都能將屬性注入到我們想要的地方去的,比如在Utils使用到dao,我們就不能直接注入了,這個時候就是我們需要封裝springContext的時候了,而ApplicationContextAware就起了關(guān)鍵性的作用。




發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 昂仁县| 宜良县| 明水县| 正镶白旗| 宜兰县| 齐齐哈尔市| 安阳县| 玉溪市| 定兴县| 全椒县| 临高县| 资兴市| 安新县| 八宿县| 眉山市| 明溪县| 苍南县| 山阴县| 旬阳县| 隆回县| 辛集市| 中山市| 易门县| 乌鲁木齐县| 万盛区| 肃南| 瓦房店市| 花莲县| 麟游县| 体育| 富锦市| 纳雍县| 天峻县| 张掖市| 澜沧| 綦江县| 九寨沟县| 靖西县| 台湾省| 突泉县| 于都县|