一、先說ServletContext
javaee標準規(guī)定了,servlet容器需要在應(yīng)用項目啟動時,給應(yīng)用項目初始化一個ServletContext作為公共環(huán)境容器存放公共信息。ServletContext中的信息都是由容器提供的。
舉例:
通過自定義contextListener獲取web.xml中配置的參數(shù)
1.容器啟動時,找到配置文件中的context-param作為鍵值對放到ServletContext中
2.然后找到listener,容器調(diào)用它的contextInitialized(ServletContextEvent event)方法,執(zhí)行其中的操作
例如:在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>
配置好之后,在該類中獲取對應(yīng)的參數(shù)信息
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")); } }
執(zhí)行流程:
web.xml在<context-param></context-param>標簽中聲明應(yīng)用范圍內(nèi)的初始化參數(shù)
1.啟動一個WEB項目的時候,容器(如:Tomcat)會去讀它的配置文件web.xml.讀兩個節(jié)點: <listener></listener> 和 <context-param></context-param>
2.緊接著,容器創(chuàng)建一個ServletContext(上下文)。在該應(yīng)用內(nèi)全局共享。
3.容器將<context-param></context-param>轉(zhuǎn)化為鍵值對,并交給ServletContext.
4.容器創(chuàng)建<listener></listener>中的類實例,即創(chuàng)建監(jiān)聽.該監(jiān)聽器必須實現(xiàn)自ServletContextListener接口
5.在監(jiān)聽中會有contextInitialized(ServletContextEvent event)初始化方法
在這個方法中獲得ServletContext = ServletContextEvent.getServletContext();
“context-param的值” = ServletContext.getInitParameter("context-param的鍵");
6.得到這個context-param的值之后,你就可以做一些操作了.注意,這個時候你的WEB項目還沒有完全啟動完成.這個動作會比所有的Servlet都要早.換句話說,這個時候,你對<context-param>中的鍵值做的操作,將在你的WEB項目完全啟動之前被執(zhí)行.
web.xml中可以定義兩種參數(shù):
一個是全局參數(shù)(ServletContext),通過<context-param></context-param>
一個是servlet參數(shù),通過在servlet中聲明 <init-param>
<param-name>param1</param-name>
<param-value>avalible in servlet init()</param-value>
</init-param>
第一種參數(shù)在servlet里面可以通過getServletContext().getInitParameter("context/param")得到
第二種參數(shù)只能在servlet的init()方法中通過this.getInitParameter("param1")取得
二、spring上下文容器配置
spring為我們提供了實現(xiàn)ServletContextListener接口的上下文初始化監(jiān)聽器:org.springframework.web.context.ContextLoaderListener
spring為我們提供的IOC容器,需要我們指定容器的配置文件,然后由該監(jiān)聽器初始化并創(chuàng)建該容器。要求你指定配置文件的地址及文件名稱,一定要使用:contextConfigLocation作為參數(shù)名稱。
<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>
該監(jiān)聽器,默認讀取/WEB-INF/下的applicationContext.xml文件。但是通過context-param指定配置文件路徑后,便會去你指定的路徑下讀取對應(yīng)的配置文件,并進行初始化。
三、spring上下文容器配置后,初始化了什么?
既然,ServletContext是由Servlet容器初始化的,那spring的ContextLoaderListener又做了什么初始化呢?
1、servlet容器啟動,為應(yīng)用創(chuàng)建一個“全局上下文環(huán)境”:ServletContext
2、容器調(diào)用web.xml中配置的contextLoaderListener,初始化WebApplicationContext上下文環(huán)境(即IOC容器),加載context-param指定的配置文件信息到IOC容器中。WebApplicationContext在ServletContext中以鍵值對的形式保存
3、容器初始化web.xml中配置的servlet,為其初始化自己的上下文信息servletContext,并加載其設(shè)置的配置信息到該上下文中。將WebApplicationContext設(shè)置為它的父容器。
4、此后的所有servlet的初始化都按照3步中方式創(chuàng)建,初始化自己的上下文環(huán)境,將WebApplicationContext設(shè)置為自己的父上下文環(huán)境。
對于作用范圍而言,在DispatcherServlet中可以引用由ContextLoaderListener所創(chuàng)建的ApplicationContext中的內(nèi)容,而反過來不行。
當Spring在執(zhí)行ApplicationContext的getBean時,如果在自己context中找不到對應(yīng)的bean,則會在父ApplicationContext中去找。這也解釋了為什么我們可以在DispatcherServlet中獲取到由ContextLoaderListener對應(yīng)的ApplicationContext中的bean。
四、spring配置時:<context:exclude-filter>的使用原因,為什么在applicationContext.xml中排除controller,而在spring-mvc.xml中incloud這個controller
既然知道了spring的啟動流程,那么web容器初始化webApplicationContext時作為公共的上下文環(huán)境,只需要將service、dao等的配置信息在這里加載,而servlet自己的上下文環(huán)境信息不需要加載。故,
[email protected]?外,
[email protected]??的組件加載進來,方便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>
本站內(nèi)容系網(wǎng)友提交或本網(wǎng)編輯轉(zhuǎn)載,其目的在于傳遞更多信息,并不代表本網(wǎng)贊同其觀點和對其真實性負責。如涉及作品內(nèi)容、版權(quán)和其它問題,請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除內(nèi)容!