風雨無阻

          Spring獲取ApplicationContext的正確方式

          前兩天聯華對單系統頻頻出現out of memory的問題,經過層層分析,終于弄明白原來瓶頸出現在Spring獲取Bean那一步上。以前的做法是在delegate層ApplicationContext context = new ClassPathXmlApplicationContext("Compare.xml"); 這樣我把log4j的debug打開后發現,每做1步操作的時候系統都會重新加載這個xml,重新創建Bean的實例,重新獲取url-mapping,這無疑是個及其錯誤的方式。      研究后發現正確的使用方式是:      首先在web.xml里配置ApplicationContext      <context-param>
                    <param-name>contextConfigLocation</param-name>
                    <param-value>/WEB-INF/applicationContext.xml</param-value>
               </context-param>     <listener>
                    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
               </listener>     然后在Action中編寫如下代碼得到Context,(我是覆蓋了Struts Action的setServlet方法,也許還有更好的方法)。     public void setServlet(ActionServlet servlet){
                   super.setServlet(servlet);
                   ServletContext servletContext = servlet.getServletContext();
                  WebApplicationContext wac =    WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
                   // get yours beans     }     這樣在啟動服務時已經加載了xml中的配置,而不用每次都重新加載,大大提高了系統的效率

          posted on 2009-04-17 16:45 秋楓故事 閱讀(1017) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2009年4月>
          2930311234
          567891011
          12131415161718
          19202122232425
          262728293012
          3456789

          導航

          統計

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          新聞檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 丽江市| 牟定县| SHOW| 普定县| 丰台区| 巴林右旗| 惠来县| 资中县| 利津县| 平乡县| 英吉沙县| 东丰县| 边坝县| 烟台市| 稷山县| 南充市| 碌曲县| 蕉岭县| 伊宁市| 玉环县| 耒阳市| 洛南县| 临安市| 德州市| 南江县| 杂多县| 天镇县| 五原县| 沈阳市| 睢宁县| 揭西县| 万源市| 惠东县| 闽清县| 齐齐哈尔市| 四会市| 江西省| 牙克石市| 易门县| 永城市| 穆棱市|