當(dāng)柳上原的風(fēng)吹向天際的時(shí)候...

          真正的快樂(lè)來(lái)源于創(chuàng)造

            BlogJava :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
            368 Posts :: 1 Stories :: 201 Comments :: 0 Trackbacks

          在WebApp中獲得XMLWebApplicationContext的步驟
          1.在Web.xml中配置上下文載入器.
          2.指定上下文載入器的配置文件.
          3.獲得應(yīng)用上下文.

          1.在Web.xml中配置上下文載入器
          根據(jù)你的系統(tǒng)情況,你可以選擇兩種上下文載入器:ContextLoaderListener和ContextLoaderServlet.如果你的Web容器支持Servlet2.3標(biāo)準(zhǔn)或更高,你可以使用兩者,否則只能使用后者.
          ContextLoaderListener在Web.xml應(yīng)該如下配置:

          <listener> 
               
          <listener-class>
                    org.springframework.web.context.ContextLoaderListener
               
          </listener-class> 
          </listener> 

          ContextLoaderServlet在Web.xml應(yīng)該如下配置:

          <servlet>
            
          <servlet-name>context</servlet-name>
            
          <servlet-class>
            org.springframework.web.context.ContextLoaderServlet
            
          </servlet-class>
            
          <load-on-startup>1</load-on-startup>
          </servlet>


          2.指定上下文載入器的配置文件

          不論你使用的那種上下文載入器,你都應(yīng)該指明Spring配置文件的位置.如果沒(méi)有指定,上下文載入器將把/web-inf/application-Context.xml當(dāng)作Spring配置文件。
          要指定Spring配置文件的位置,你可以在Servlet上下文設(shè)置contextConfigLocation參數(shù)來(lái)為上下文載入器指定一個(gè)或多個(gè)Spring配置文件(使用通配符或是用逗號(hào)隔開(kāi))。如下所示:

          <context-param>
              
          <param-name>
                   contextConfigLocation
             
          </param-name>
             
          <param-value>
                    /WEB-INF/cfg/bean.xml
             
          </param-value>
          </context-param>

           

          3.獲得應(yīng)用上下文
          接下來(lái)我們就可以獲得ApplicationContext了,代碼如下:

          WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext); 

          在一個(gè)自啟動(dòng)的Servlet中,我們可以這樣獲得它:

          public class InitialSystemServlet extends HttpServlet {
            
          public void init(ServletConfig config) throws ServletException {
              
          // 取得Spring的上下文
              WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(config.getServletContext());
              ..
            }

            
            ..
          }

          如果把獲得的上下文的地址給一個(gè)靜態(tài)引用,我們以后就可以在應(yīng)用中的任意位置使用ApplicationContext了。

          posted on 2008-11-24 23:19 何楊 閱讀(8652) 評(píng)論(1)  編輯  收藏

          Feedback

          # re: Spring在Web應(yīng)用中的配置[未登錄](méi) 2013-08-22 22:40 ddd
          dfdffddf  回復(fù)  更多評(píng)論
            


          只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 陵川县| 睢宁县| 南召县| 通渭县| 绥德县| 铜梁县| 井研县| 桃园县| 天台县| 六枝特区| 公主岭市| 龙南县| 岳阳县| 西林县| 隆昌县| 平利县| 渭源县| 沐川县| 永平县| 封丘县| 揭西县| 上思县| 驻马店市| 宣城市| 广宗县| 澎湖县| 和硕县| 枣阳市| 沙河市| 茂名市| 南安市| 甘洛县| 铁力市| 子洲县| 霍林郭勒市| 文安县| 灵石县| 金华市| 茶陵县| 贵德县| 鄂伦春自治旗|