Spring-MVC理解之一:應用上下文webApplicationContext

來源:互聯網
上載者:User

標籤:方式   default   extc   檔案的   ioc   java   lis   config   listen   

原文轉至  http://www.cnblogs.com/brolan...

 

 

一、先說ServletContext

  javaee標準規定了,servlet容器需要在應用項目啟動時,給應用項目初始化一個ServletContext作為公用環境容器存放公用資訊。ServletContext中的資訊都是由容器提供的。

舉例:

通過自訂contextListener擷取web.xml中配置的參數1.容器啟動時,找到設定檔中的context-param作為索引值對放到ServletContext中2.然後找到listener,容器調用它的contextInitialized(ServletContextEvent event)方法,執行其中的操作例如:在web.xml中配置
<context-param>   <param-name>key</param-name>   <param-value>value123</param-value></context-param><listener>    <listener-class>com.brolanda.contextlistener.listener.ContextListenerTest</listener-class></listener>
配置好之後,在該類中擷取對應的參數資訊
package com.brolanda.contextlistener.listener;import javax.servlet.ServletContext;import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;public class ContextListenerTest implements ServletContextListener {        public void contextDestroyed(ServletContextEvent event) {        System.out.println("*************destroy ContextListener*************");    }        @SuppressWarnings("unused")    public void contextInitialized(ServletContextEvent event) {        System.out.println("*************init ContextListener*************");        ServletContext servletContext = event.getServletContext();        System.out.println("key:"+servletContext.getInitParameter("key"));    }    }

執行流程:

  web.xml在<context-param></context-param>標籤中聲明應用範圍內的初始化參數

1.啟動一個WEB項目的時候,容器(如:Tomcat)會去讀它的設定檔web.xml.讀兩個節點: <listener></listener> 和 <context-param></context-param>2.緊接著,容器建立一個ServletContext(上下文)。在該應用內全域共用。

3.容器將<context-param></context-param>轉化為索引值對,並交給ServletContext.

4.容器建立<listener></listener>中的類執行個體,即建立監聽.該監聽器必須實現自ServletContextListener介面

5.在監聽中會有contextInitialized(ServletContextEvent event)初始化方法

              在這個方法中獲得ServletContext = ServletContextEvent.getServletContext();
            “context-param的值” = ServletContext.getInitParameter("context-param的鍵");6.得到這個context-param的值之後,你就可以做一些操作了.注意,這個時候你的WEB項目還沒有完全啟動完成.這個動作會比所有的Servlet都要早.換句話說,這個時候,你對<context-param>中的索引值做的操作,將在你的WEB項目完全啟動之前被執行.  web.xml中可以定義兩種參數:    一個是全域參數(ServletContext),通過<context-param></context-param>    一個是servlet參數,通過在servlet中聲明        <init-param>                                                                          <param-name>param1</param-name>                                                                          <param-value>avalible in servlet init()</param-value>                                                                       </init-param>      第一種參數在servlet裡面可以通過getServletContext().getInitParameter("context/param")得到     第二種參數只能在servlet的init()方法中通過this.getInitParameter("param1")取得 

二、spring上下文容器配置

  spring為我們提供了實現ServletContextListener介面的上下文初始化監聽器:org.springframework.web.context.ContextLoaderListener

  spring為我們提供的IOC容器,需要我們指定容器的設定檔,然後由該監聽器初始化並建立該容器。要求你指定設定檔的地址及檔案名稱,一定要使用:contextConfigLocation作為參數名稱。

<context-param>    <param-name>contextConfigLocation</param-name>    <param-value>/WEB-INF/applicationContext.xml,/WEB-INF/action-servlet.xml,/WEB-INF/jason-servlet.xml</param-value></context-param><listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>

該監聽器,預設讀取/WEB-INF/下的applicationContext.xml檔案。但是通過context-param指定設定檔路徑後,便會去你指定的路徑下讀取對應的設定檔,並進行初始化。

三、spring上下文容器配置後,初始化了什嗎?

  既然,ServletContext是由Servlet容器初始化的,那spring的ContextLoaderListener又做了什麼初始化呢?

        1、servlet容器啟動,為應用建立一個“全域上下文環境”:ServletContext        2、容器調用web.xml中配置的contextLoaderListener,初始化WebApplicationContext上下文環境(即IOC容器),載入context-param指定的設定檔資訊到IOC容器中。WebApplicationContext在ServletContext中以索引值對的形式儲存        3、容器初始化web.xml中配置的servlet,為其初始化自己的上下文資訊servletContext,並載入其設定的配置資訊到該上下文中。將WebApplicationContext設定為它的父容器。        4、此後的所有servlet的初始化都按照3步中方式建立,初始化自己的上下文環境,將WebApplicationContext設定為自己的父上下文環境。 

 

       對於作用範圍而言,在DispatcherServlet中可以引用由ContextLoaderListener所建立的ApplicationContext中的內容,而反過來不行。       當Spring在執行ApplicationContext的getBean時,如果在自己context中找不到對應的bean,則會在父ApplicationContext中去找。這也解釋了為什麼我們可以在DispatcherServlet中擷取到由ContextLoaderListener對應的ApplicationContext中的bean。

 

 四、spring配置時:<context:exclude-filter>的使用原因,為什麼在applicationContext.xml中排除controller,而在spring-mvc.xml中incloud這個controller

    既然知道了spring的啟動流程,那麼web容器初始化webApplicationContext時作為公用的上下文環境,只需要將service、dao等的配置資訊在這裡載入,而servlet自己的上下文環境資訊不需要載入。故,在applicationContext.xml中將@Controller注釋的組件排除在外,而在dispatcherServlet載入的設定檔中將@Controller注釋的組件載入進來,方便dispatcherServlet進行控制和尋找。故,配置如下: applicationContext.mxl中: <context:component-scan base-package="com.linkage.edumanage">      <context:exclude-filter expression="org.springframework.stereotype.Controller"    type="annotation" />  </context:component-scan> spring-mvc.xml中:  <context:component-scan base-package="com.brolanda.cloud"   use-default-filters="false">       <context:include-filter expression="org.springframework.stereotype.Controller"    type="annotation" />  </context:component-scan>

Spring-MVC理解之一:應用上下文webApplicationContext

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.