每一個整合spring架構的項目中,總是不可避免地要在web.xml中加入這樣一段配置。
<!-- Spring配置檔案開始 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring-config.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring配置檔案結束 --
而這段配置有什麼作用,或者說ContextLoaderListener到底有什麼作用。表示疑惑,我們研究一下ContextLoaderListener源碼。
public class ContextLoaderListener extends ContextLoader implements ServletContextListener
ContextLoaderListener繼承自ContextLoader,實作的是ServletContextListener接口。
繼承ContextLoader有什麼作用?
ContextLoaderListener可以指定在Web應用程式啟動時載入Ioc容器,正是通過ContextLoader來實作的,ContextLoader來完成實際的WebApplicationContext,也就是Ioc容器的初始化工作。
實作ServletContextListener又有什麼作用?
ServletContextListener接口裡的函數會結合Web容器的生命周期被調用。因為ServletContextListener是ServletContext的監聽者,如果ServletContext發生變化,會觸發相應的事件,而監聽器一直對事件監聽,如果接收到了變化,就會做出預先設計好的相應動作。由于ServletContext變化而觸發的監聽器的響應具體包括:在伺服器啟動時,ServletContext被建立的時候,伺服器關閉時,ServletContext将被銷毀的時候等。
那麼ContextLoaderListener的作用是什麼?
ContextLoaderListener的作用就是啟動Web容器時,讀取在contextConfigLocation中定義的xml檔案,自動裝配ApplicationContext的配置資訊,并産生WebApplicationContext對象,然後将這個對象放置在ServletContext的屬性裡,這樣我們隻要得到Servlet就可以得到WebApplicationContext對象,并利用這個對象通路spring容器管理的bean。
簡單來說,就是上面這段配置為項目提供了spring支援,初始化了Ioc容器。
那又是怎麼為我們的項目提供spring支援的呢?
上面說到“監聽器一直對事件監聽,如果接收到了變化,就會做出預先設計好的相應動作”。而監聽器的響應動作就是在伺服器啟動時contextInitialized會被調用,關閉的時候contextDestroyed被調用。這裡我們關注的是WebApplicationContext如何完成建立。是以銷毀方法就暫不讨論。
@Override
public void contextInitialized(ServletContextEvent event) {
//初始化webApplicationCotext</font>
initWebApplicationContext(event.getServletContext());
值得一提的是在initWebApplicationContext方法上面的注釋提到(請對照原注釋),WebApplicationContext根據在context-params中配置contextClass和contextConfigLocation完成初始化。有大概的了解後,接下來繼續研究源碼。
public WebApplicationContext initWebApplicationContext(
ServletContext servletContext) {
// application對象中存放了spring context,則抛出異常
// 其中ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE = WebApplicationContext.class.getName() + ".ROOT";
if (servletContext
.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) {
throw new IllegalStateException(
"Cannot initialize context because there is already a root application context present - "
+ "check whether you have multiple ContextLoader* definitions in your web.xml!");
}
// 建立得到WebApplicationContext
// createWebApplicationContext最後傳回值被強制轉換為ConfigurableWebApplicationContext類型
if (this.context == null) {
this.context = createWebApplicationContext(servletContext);
}
// 隻要上一步強轉成功,進入此方法(事實上走的就是這條路)
if (this.context instanceof ConfigurableWebApplicationContext) {
// 強制轉換為ConfigurableWebApplicationContext類型
ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) this.context;
// cwac尚未被激活,目前還沒有進行配置檔案加載
if (!cwac.isActive()) {
// 加載配置檔案
configureAndRefreshWebApplicationContext(cwac, servletContext);
【點選進入該方法發現這樣一段:
//為wac綁定servletContext
wac.setServletContext(sc);
//CONFIG_LOCATION_PARAM=contextConfigLocation
//getInitParameter(CONFIG_LOCATION_PARAM)解釋了為什麼配置檔案中需要有contextConfigLocation項
//需要注意還有sevletConfig.getInitParameter和servletContext.getInitParameter作用範圍是不一樣的
String initParameter = sc.getInitParameter(CONFIG_LOCATION_PARAM);
if (initParameter != null) {
//裝配ApplicationContext的配置資訊
wac.setConfigLocation(initParameter);
}
】
}
}
// 把建立好的spring context,交給application内置對象,提供給監聽器/過濾器/攔截器使用
servletContext.setAttribute(
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,
this.context);
// 傳回webApplicationContext
return this.context;
nitWebApplicationContext中加載了contextConfigLocation的配置資訊,初始化Ioc容器,說明了上述配置的必要性。而我有了新的疑問。
WebApplicationContext和ServletContext是一種什麼樣的關系呢?
翻到源碼,發現在ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE上面有:
org.springframework.web.context.support.WebApplicationContextUtils#getWebApplicationContext
org.springframework.web.context.support.WebApplicationContextUtils#getRequiredWebApplicationContex