原博地址:https://www.jianshu.com/p/523bfddf0810
每一個整合spring框架的項目中,總是不可避免地要在web.xml中加入這樣一段配置。
<!-- 配置spring核心監聽器,默認會以 /WEB-INF/applicationContext.xml作為配置文件 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- contextConfigLocation參數用來指定Spring的配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext*.xml</param-value> </context-param>
這段配置有何作用呢,通過ContextLoaderListener源碼來分析一下
public class ContextLoaderListener extends ContextLoader implements ServletContextListener
ContextLoaderListener繼承自ContextLoader,實現的是ServletContextListener接口。
ContextLoaderListener可以指定在Web應用程序啟動時載入Ioc容器,正是通過ContextLoader來實現的,可以說是Ioc容器的初始化工作。
ServletContextListener接口里的函數會結合Web容器的生命周期被調用。因為ServletContextListener是ServletContext的監聽者,如果ServletContext發生變化,會觸發相應的事件,而監聽器一直對事件監聽,如果接收到了變化,就會做出預先設計好的相應動作。由於ServletContext變化而觸發的監聽器的響應具體包括:在服務器啟動時,ServletContext被創建的時候,服務器關閉時,ServletContext將被銷毀的時候等,相當於web的生命周期創建與效果的過程。
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; }
initWebApplicationContext中加載了contextConfigLocation的配置信息,初始化Ioc容器,說明了上述配置的必要性。而我有了新的疑問。
WebApplicationContext和ServletContext是一種什么樣的關系呢?
翻到源碼,發現在ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE上面有:
org.springframework.web.context.support.WebApplicationContextUtils#getWebApplicationContext
org.springframework.web.context.support.WebApplicationContextUtils#getRequiredWebApplicationContext
順藤摸瓜來到WebApplicationContextUtils,發現getWebApplicationContext方法中只有一句話:
return getWebApplicationContext(sc,WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
感覺在這個返回方法中肯定有解決我問題的答案,於是繼續往下查找。
Object attr = sc.getAttribute(attrName); return (WebApplicationContext) attr;
這不就是initWebApplicationContext方法中setAttribute進去的WebApplicationContext嗎?因此可以確信得到servletContext也可以得到webApplicationContext。
那么問題又來了,通過servletContext可以得到webApplicationContext有什么意義嗎?
上面我們提到“把創建好的springcontext,交給application內置對象,提供給監聽器/過濾器/攔截器使用”。
假設我們有一個需求是要做首頁顯示。平時的代碼經常是在控制器控制返回結果給前台的,那么第一頁需要怎么去顯示呢。抽象得到的問題是如何在一開始拿到數據。
能想到的大致的解決方案有三種:
+++++++++++++++++++++++++++++++++++++++++++++++
1.可以通過ajx異步加載的方式請求后台數據,然后呈現出來。
+++++++++++++++++++++++++++++++++++++++++++++++
2.頁面重定向的思路,先把查詢請求交給控制器處理,得到查詢結果后轉到首頁綁定數據並顯示。
+++++++++++++++++++++++++++++++++++++++++++++++
3.在Ioc容器初始化的過程中,把數據查詢出來,然后放在application里。
+++++++++++++++++++++++++++++++++++++++++++++++
三種方案都能實現首頁顯示,不過前兩種方法很大的弊端就是需要頻繁操作數據庫,會對數據庫造成一定的壓力。而同樣地實現監聽器邏輯的第三種方法也有弊端。就是無法實時更新,不過數據庫壓力相對前兩種不是很大。針對無法實時更新這一問題有成熟的解決方案,可以使用定時器的思路。隔一段時間重啟一次。目前來說有許多網站都是這么做的。
而對於首頁這種訪問量比較大的頁面,如果說最好的解決方案是實現靜態化技術。
前陣子考慮寫一篇關於偽靜態化的文章。當然和靜態化還是有區別的。好了,回到我們listener的實現上來。
我們說過“ContextLoaderListener實現了ServletContextListener接口。服務器啟動時contextInitialized會被調用”。加載容器時能取出數據,那么我們需要實現這個接口。
@Service public class CommonListener implements ServletContextListener{ @Autowired private UserService userService; public void contextInitialized(ServletContextEvent servletContextEvent) { //Exception sending context initialized event to listener instance of class com.walidake.listener.CommonListener java.lang.NullPointerException System.out.println(userService.findUser()); } public void contextDestroyed(ServletContextEvent servletContextEvent) { // TODO Auto-generated method stub }
}
需要注意一件事!
spring是管理邏輯層和數據訪問層的依賴。而listener是web組件,那么必然不能放在spring里面。真正實例化它的應該是tomcat,在啟動加載web.xml實例化的。上層的組件不可能被下層實例化得到。
因此,即使交給Spring實例化,它也沒能力去幫你實例化。真正實現實例化的還是web容器。
然而NullPointerException並不是來自這個原因,我們說過“ContextLoader來完成實際的WebApplicationContext,也就是Ioc容器的初始化工作”。我們並沒有繼承ContextLoader,沒有Ioc容器的初始化,是無法實現依賴注入的。
因此,我們想到另一種解決方案,能不能通過new ClassPathXmlApplicationContext的方式,像測試用例那樣取得Ioc容器中的bean對象。
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-config.xml"); userService = context.getBean(UserService.class); System.out.println(userService.findUser());
發現可以正常打印出結果。然而觀察日志后發現,原本的單例被創建了多次(譬如userServiceImpl等)。因此該方法並不可取。
那么,由於被創建了多次,是不是可以說明項目中已存在了WebApplicationContext?
是的。我們一開始說“在初始化ContextLoaderListener成功后,spring context會存放在servletContext中”,意味着我們完全可以從servletContext取出WebApplicationContext,然后getBean取得需要的bean對象。
所以完全可以這么做。
ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(servletContextEvent.getServletContext()); userService = context.getBean(UserService.class); datas = userService.findUser(); servletContextEvent.getServletContext().setAttribute("datas", datas);
然后在jsp頁面通過jstl打印出來。結果如下:

顯示結果正確,並且再次觀察日志發現並沒有初始化多次,說明猜想和實現都是正確的。