ServletContext
Web容器在啟動(dòng)的過程中,會(huì)為每個(gè)Web應(yīng)用程序創(chuàng)建一個(gè)對(duì)應(yīng)的ServletContext對(duì)象,它代表了當(dāng)前的Web應(yīng)用,為Spring IoC容器提供宿主環(huán)境。
在部署Web工程的時(shí)候,Web容器會(huì)讀取web.xml,創(chuàng)建ServletContext,當(dāng)前Web工程所有部分都共享這個(gè)Context。context-param為ServletContext提供鍵值對(duì),即Servlet上下文的信息,這些信息Listener、Filter和Servlet都有可能使用到,因此先加載context-param,創(chuàng)建ServletContext,然后加載Listener,再加載Filter,最后加載Servlet。
接下來我將按照這個(gè)加載順序來分析Spring容器的啟動(dòng)過程。
ContextLoaderListener
web.xml中配置有ContextLoaderListener,也可以自定義一個(gè)實(shí)現(xiàn)了ServletContextListener接口的Listener類,web.xml中的配置實(shí)例如下。
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Web容器在啟動(dòng)的過程中會(huì)觸發(fā)ServletContextEvent事件,會(huì)被ContextLoaderListener監(jiān)聽到,并調(diào)用ContextLoaderListener中的contextInitialized方法,contextInitialized方法如下所示。
public void contextInitialized(ServletContextEvent event) {
this.initWebApplicationContext(event.getServletContext());
}
ContextLoaderListener類繼承了ContextLoader,在初始化Context的過程中,調(diào)用ContextLoader的initWebApplicationContext方法初始化WebApplicationContext。WebApplicationContext是一個(gè)接口,Spring默認(rèn)的實(shí)現(xiàn)類為XmlWebApplicationContext,XmlWebApplicationContext就是Spring的IoC容器。
在初始化XmlWebApplicationContext之前,Web容器已經(jīng)加載了context-param,web.xml中的context-param實(shí)例如下所示。作為Spring的IoC容器,其對(duì)應(yīng)的Bean定義的配置正是context-param指定的。
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
接著進(jìn)入到initWebApplicationContext方法內(nèi),initWebApplicationContext方法定義如下(已省略部分代碼)。
public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {
if(servletContext.getAttribute(
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) {
throw new IllegalStateException("...");
}else{
if(this.context == null) {
this.context = this.createWebApplicationContext(servletContext);
}
}
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
}
在Spring IoC容器初始化前,initWebApplicationContext先檢測以ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE為key的值是否為空,若不為空,則初始化IoC Context,并在初始化完畢后,以ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE為key將IoC Context存儲(chǔ)到ServletContext中。
初始化Servlet
Servlet可以在web.xml中配置多個(gè),在Spring中,最基本的Servlet為DispatcherServlet,對(duì)應(yīng)的配置實(shí)例如下所示。
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/appServlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
DispatcherServlet會(huì)建立自己的IoC Context,用以持有相關(guān)的Bean,在初始化自己的IoC Context的過程中,先通過WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,從ServletContext中獲取WebApplicationContext,將WebApplicationContext作為DispatcherServlet的IoC Context的 parent Context。DispatcherServlet自己的IoC Context的初始化工作在DispatcherServlet的initStrategies方法中完成,包括控制器映射,視圖解析等,initStrategies方法如下所示。
protected void initStrategies(ApplicationContext context) {
this.initMultipartResolver(context);
this.initLocaleResolver(context);
this.initThemeResolver(context);
this.initHandlerMappings(context);
this.initHandlerAdapters(context);
this.initHandlerExceptionResolvers(context);
this.initRequestToViewNameTranslator(context);
this.initViewResolvers(context);
this.initFlashMapManager(context);
}
DispatcherServlet自己的IoC Context的類型也是XmlWebApplicationContext,初始化完畢后,Spring將以與DispatcherServlet的servlet-name屬性相關(guān)的符號(hào)作為key,將IoC Context保存到 ServletContext中。這樣每個(gè)Servlet就都可以持有自己的Context,也就是都擁有自己的Bean空間,同時(shí),各個(gè)Servlet之間還共享著key為WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE的WebApplicationContext,其中定義的Bean為各個(gè)Servlet共享的Bean。
參考:segmentfault
轉(zhuǎn)載自:Spring IoC Context啟動(dòng)過程解析