當柳上原的風吹向天際的時候...

          真正的快樂來源于創造

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            368 Posts :: 1 Stories :: 201 Comments :: 0 Trackbacks

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

          1.在Web.xml中配置上下文載入器
          根據你的系統情況,你可以選擇兩種上下文載入器:ContextLoaderListener和ContextLoaderServlet.如果你的Web容器支持Servlet2.3標準或更高,你可以使用兩者,否則只能使用后者.
          ContextLoaderListener在Web.xml應該如下配置:

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

          ContextLoaderServlet在Web.xml應該如下配置:

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


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

          不論你使用的那種上下文載入器,你都應該指明Spring配置文件的位置.如果沒有指定,上下文載入器將把/web-inf/application-Context.xml當作Spring配置文件。
          要指定Spring配置文件的位置,你可以在Servlet上下文設置contextConfigLocation參數來為上下文載入器指定一個或多個Spring配置文件(使用通配符或是用逗號隔開)。如下所示:

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

           

          3.獲得應用上下文
          接下來我們就可以獲得ApplicationContext了,代碼如下:

          WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext); 

          在一個自啟動的Servlet中,我們可以這樣獲得它:

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

            
            ..
          }

          如果把獲得的上下文的地址給一個靜態引用,我們以后就可以在應用中的任意位置使用ApplicationContext了。

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

          Feedback

          # re: Spring在Web應用中的配置[未登錄] 2013-08-22 22:40 ddd
          dfdffddf  回復  更多評論
            


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


          網站導航:
           
          主站蜘蛛池模板: 江北区| 大冶市| 佛教| 阳西县| 仁布县| 乐陵市| 鄱阳县| 宜城市| 郴州市| 论坛| 穆棱市| 广州市| 庆城县| 滦平县| 大连市| 无锡市| 安图县| 广宗县| 基隆市| 屯留县| 开远市| 广河县| 秭归县| 兴化市| 金秀| 望都县| 和田县| 汶川县| 望奎县| 湘阴县| 多伦县| 札达县| 天祝| 应城市| 北碚区| 桓仁| 内黄县| 禄劝| 伊金霍洛旗| 张家川| 张家口市|