<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    牙牙窩

    BlogJava 聯系 聚合 管理
      8 Posts :: 21 Stories :: 10 Comments :: 0 Trackbacks

    ?

    一、??????? 概述

    Spring MVC 的開發是基于 action-servlet.xml 進行配置,但不支持開發模式下進行動態的配置文件載入。本文主要是介紹如何修改 Spring 的源代碼,使 Spring 支持動態的配置文件更新,讓開發變得更加簡單。

    二、??????? 實現 action-servlet.xml 動態載入

    ??? Spring 提取配置文件的思路 :每次 Spring MVC 會在使用前將 XML 文件載入內存中,并生成映射類的實例,放在 Mapping Map 里。然后判斷每個請求,如果有其 URL 所對應的映射,則返回其對應的 Action 實例。

    ??? 修改思路 :將每次得到請求時,讓程序重新載入 xml 文件,并實例化其映射,然后放入 Mapping Map 中。

    1、???????????? 首先是 FrameworkServlet ,他是 DispatcherServlet 的基類。 XML 在載入內存后,放在一個叫 WebApplicationContext 的類中。找到 getWebApplicationContext() 方法,加入以下代碼:

    ?????? ConfigurableWebApplicationContext wac = (ConfigurableWebApplicationContext) BeanUtils

    ????????????? .instantiateClass(getContextClass());

    ?????? wac.setParent(WebApplicationContextUtils

    ????????????? .getWebApplicationContext(getServletContext()));

    ?????? wac.setServletContext(getServletContext());

    ?????? wac.setNamespace(getNamespace());

    ?????? if (getContextConfigLocation() != null ) {

    ?????????? wac

    ????????????????? .setConfigLocations(StringUtils

    ???????????????????????? .tokenizeToStringArray(

    ??????????????????????????????? getContextConfigLocation(),

    ??????????????????????????? ??? ConfigurableWebApplicationContext. CONFIG_LOCATION_DELIMITERS ));

    ?????? }

    ?????? wac.refresh();

    ?????? this . webApplicationContext = wac;

    這樣每次再讀取 WebApplicationContext 的時候,會重新載入 XML 文件一次。

    ?

    2、???????????? 修改 DispatcherServlet ,這個 Servlet 是處理所有請求的入口。找到 getHandler() 這個方法,他負責找到相應的 Action ,并返回其實例。將代碼中的

    ?????? Iterator it = this.handlerMappings.iterator();

    ?????? while (it.hasNext()) {

    ?????????? HandlerMapping hm = (HandlerMapping) it.next();

    ?????????? if (logger.isDebugEnabled()) {

    ????????????? logger.debug("Testing handler map [" + hm? + "] in DispatcherServlet with name '" +

    ???????????????????? getServletName() + "'");

    ?????????? }

    ?????????? handler = hm.getHandler(request);

    ?????????? if (handler != null) {

    ????????????? if (cache) {

    ????????????????? request.setAttribute(HANDLER_EXECUTION_CHAIN_ATTRIBUTE, handler);

    ????????????? }

    ????????????? return handler;

    ?????????? }

    ?????? }

    改為

    ?????? initHandlerMappings();

    ??????

    ?????? Iterator it = this . handlerMappings .iterator();

    ?????? while (it.hasNext()) {

    ?????????? BeanNameUrlHandlerMapping hm = (BeanNameUrlHandlerMapping) it.next();

    ?????????? if ( logger .isDebugEnabled()) {

    ????????????? logger .debug( "Testing handler map [" + hm? + "] in DispatcherServlet with name '" +

    ???????????????????? getServletName() + "'" );

    ?????????? }

    ?????????? hm.initApplicationContext();

    ?????????? handler = hm.getHandler(request);

    ?????????? if (handler != null ) {

    ????????????? if (cache) {

    ????????????????? request.setAttribute( HANDLER_EXECUTION_CHAIN_ATTRIBUTE , handler);

    ????????????? }

    ????????????? return handler;

    ?????????? }

    ?????? }

    注解:

    1)?? 其中 BeanNameUrlHandlerMapping 是將強制轉換 HandlerMapping 時,用子類代替父類,因為子類提供了一個重新初始化的方法 initApplicationContext() ,調用該方法可以重新載入 WebApplicationContext , 并刷新 Mapping Map

    2)?????? initHandlerMappings() DispatcherServlet 初始化 Mapping 的一個方法。在生成 WebApplicationContext 時,程序還會把放在 ApplicationObjectSupport.applicationContext 保存,因此需要重新初始化一次。

    ?

    3 、修改 org.springframework.web.servlet.handler.AbstractUrlHandlerMapping

    類中的 registerHandler() 方法,它的作用是注冊 Mapping ,去掉重復性校驗,將下面幾行代碼注釋掉。

    ??? if (mappedHandler != null) {

    ??????? throw new ApplicationContextException(

    ?????????????? "Cannot map handler [" + handler + "] to URL path [" + urlPath +

    ?????????????? "]: there's already handler [" + mappedHandler + "] mapped");

    ??? }

    ?

    ?

    三、實現 applicationContext.xml 的動態載入

    ??? Spring 實現思路: applicationContext.xml Spring 默認的配置文件,它利用配置 ContextLoaderListener 的方式,在應用載入時啟動,并將 applicationContext.xml 載入內存中,放在 ServletContext Attribute 中,保存的方式是一個 WebApplicationContext 類。當每次調用類時, beanFactory 會調用 WebApplicationContextUtils 中的方法 getWebApplicationContext() ,得到配置信息。

    ??? 修改方法: ContextLoaderListener 初始化 WebApplicationContext 時,會利用 ContextLoader 提供的方法 initWebApplicationContext() 進行初始化,我們只需要得到 Listener 的這個 ContextLoader 的實例,并重新調用一個初始化的方法就可以實現重新載入了。

    ??? 修改步驟:

    1 、找到 ContextLoaderListener 類的方法 contextInitialized() ,在 Context 初始化的時候將 ContextLoader 的引用放在 ServletContext Attribute 中:

    public void contextInitialized(ServletContextEvent event) {

    ?????? this . contextLoader = createContextLoader();

    ?????? this . contextLoader .initWebApplicationContext(event.getServletContext());

    ?????? event.getServletContext().setAttribute( "ListenerContextLoader" , this . contextLoader );

    }

    注: "ListenerContextLoader" 是自定義的名稱,可以任意修改。

    ?

    3、???????????? 找到 WebApplicationContextUtils 類的方法 getWebApplicationContext() ,修改第一行代碼:

    Object attr = sc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

    改為:

    ?????? Object attr = null ;

    ?????? ContextLoader cl = (ContextLoader) sc

    ????????????? .getAttribute( "ListenerContextLoader" );

    ?????? if (cl != null )

    ?????????? attr = cl.initWebApplicationContext(sc);

    這樣,在每次獲取 WebApplicationContext 時,程序會重新載入 applicationContext.xml 一次。

    ?

    OK !大功告成, Enjoy your spring developing !!!

    ?

    posted on 2006-12-06 09:38 大牙 閱讀(9669) 評論(7)  編輯  收藏 所屬分類: 架構師歷程

    Feedback

    # re: 關于Spring配置文件的動態載入的修改 2007-11-19 14:38 tedeyang
    good,樓主很能鉆。
    不過用監視xml文件的方式更好一點,否則刷新得太頻繁了。  回復  更多評論
      

    # re: 關于Spring配置文件的動態載入的修改 2007-11-19 14:38 tedeyang
    另外在bean依賴關系復雜的時候會比較麻煩。  回復  更多評論
      

    # re: 關于Spring配置文件的動態載入的修改 2008-03-27 11:25 疑問
    我修改了怎么不成功呢?   回復  更多評論
      

    # re: 關于Spring配置文件的動態載入的修改 2009-07-09 11:12 hezhuo1985
    @tedeyang
    監視xml文件是怎樣實現的?
      回復  更多評論
      

    # re: 關于Spring配置文件的動態載入的修改 2009-07-09 11:50 hezhuo1985
    實現 applicationContext.xml 的動態載入 ,具體怎么操作?
      回復  更多評論
      

    # re: 關于Spring配置文件的動態載入的修改 2011-12-08 10:48 陳華
    請問樓主。如何不修改源碼實現這樣的需求:

    在服務器運行的狀態,修改Spring中的數據庫連接,讓它重新加載。萬分感謝!

    QQ 705541917

    chengongmo@126.com  回復  更多評論
      

    # re: 關于Spring配置文件的動態載入的修改 2012-02-15 15:20 jakey766
    每次進入DispatcherServlet 時重新加載一下不太合理呀,LZ現在有其他方法沒?spring中的那幾個方法都用final修飾了,想改都改不了啊  回復  更多評論
      

    主站蜘蛛池模板: 污污视频免费观看网站| MM1313亚洲精品无码久久| 亚洲xxxxxx| 激情无码亚洲一区二区三区| 免费无码国产V片在线观看| 久久国产精品免费一区二区三区| 精品视频在线免费观看| 真人做A免费观看| 国产成人免费一区二区三区| 亚洲色婷婷综合久久| 亚洲福利电影在线观看| 久久亚洲中文字幕无码| 9久久免费国产精品特黄| 亚洲免费网站在线观看| 在线免费观看韩国a视频| 国产亚洲精品国看不卡| 亚洲综合精品一二三区在线| 亚洲自偷自偷在线成人网站传媒| 免费大片av手机看片| 日韩免费无码视频一区二区三区| 亚洲人成电影网站免费| 亚洲一区二区视频在线观看| 久久精品国产亚洲AV无码娇色 | 亚洲精品欧洲精品| 亚洲AV无码一区二区三区电影| 皇色在线免费视频| 日韩精品无码区免费专区| 国产乱辈通伦影片在线播放亚洲| 伊人久久综在合线亚洲2019| 国产AV无码专区亚洲AV琪琪| 免费国产黄网站在线观看| 国产成人无码免费视频97| 亚洲第一精品福利| 久久久久久久久无码精品亚洲日韩| 日本免费人成网ww555在线| 青青青青青青久久久免费观看| 亚洲三级电影网址| 久久精品国产亚洲AV久| 国产免费久久久久久无码| 免费精品人在线二线三线区别| 亚洲色一色噜一噜噜噜|