一、先說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>