javascript
Spring mvc ContextLoaderListener 原理解析
對于熟悉Spring MVC功能,首先應從web.xml 開始,在web.xml 文件中我們需要配置一個監聽器 ContextLoaderListener,如下。
<!-- 加載spring上下文信息,最主要的功能是解析applicationContext.xml --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置applicationContext.xml的位置路徑,讓ContextLoaderListener進行加載該配置文件,并解析 --> <context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value> </context-param>ContextLoaderListener有什么用?提供什么功能呢?我們下面通過源碼分析下。
ContextLoaderListener 源碼分析
public class ContextLoaderListener extends ContextLoader implements ServletContextListener {public ContextLoaderListener() {}public ContextLoaderListener(WebApplicationContext context) {super(context);}//web 容器啟動時執行@Overridepublic void contextInitialized(ServletContextEvent event) {//初始化WebApplicationContext對象initWebApplicationContext(event.getServletContext());}//web容器銷毀時執行@Overridepublic void contextDestroyed(ServletContextEvent event) {closeWebApplicationContext(event.getServletContext());ContextCleanupListener.cleanupAttributes(event.getServletContext());} }ContextLoaderListener 實現了ServletContextListener 接口。
ServletContextListener 接口我們知道是web容器創建的時候開始執行contextInitialized() 方法,web容器銷毀時執行contextDestroyed() 方法。那我們就從contextInitialized() 方法開始分析。
contextInitialized() 方法
public void contextInitialized(ServletContextEvent event) {//初始化WebApplicationContext對象initWebApplicationContext(event.getServletContext()); }initWebApplicationContext() 方法
createWebApplicationContext()
1. 調用 determineContextClass() 方法獲取WebApplicationContext的Class實例
2. 通過反射創建WebApplicationContext對象,并返回。
determineContextClass()
1. 首先判斷 servletContext中是否存在contextClass,如果有則直接返回該Class 實例(默認是沒有配置該值的)
2. 從defaultStrategies中通過WebApplicationContext全類名(包名和類名)獲取要實現WebApplicationContext接口的類。
defaultStrategies
從當前類的路徑下查找 ContextLoader.properties 并加載文件中的鍵值存儲到 defaultStrategies中。
ContextLoader.properties
# Default WebApplicationContext implementation class for ContextLoader. # Used as fallback when no explicit context implementation has been specified as context-param. # Not meant to be customized by application developers.org.springframework.web.context.WebApplicationContext=org.springframework.web.context.support.XmlWebApplicationContext通過WebApplicationContext全類名獲取它的實現類是XmlWebApplicationContext。
configureAndRefreshWebApplicationContext()
1. 獲取web.xml中配置的applicationContext.xml路徑
2. 調用refresh()進行加載xml,解析bean。
總結
通過代碼分析ContextLoaderListener的作用就是啟動Web容器的時候,初始化WebApplicationContext實例,并存放到ServletContext中。
ContextLoaderListener實現了ServletContextListener接口,在Web容器啟動的時會默認執行它的contextInitialized() 方法,然后獲取WebApplicationContext的實現類 XmlWebApplicationContext,并實例化后存放到ServletContext中,通過XmlWebApplicationContext類進行加載、解析applicationContext.xml 配置文件。ServletContext在整個Web容器范圍內都有效,都可以獲取得到。
XmlWebApplicationContext類的作用
通過代碼可以大概了解到就是讀取web.xml中配置的applicationContext.xml ,然后加載解析Bean信息。
想了解更多精彩內容請關注我的公眾號
本人簡書blog地址:http://www.jianshu.com/u/1f0067e24ff8????
點擊這里快速進入簡書
GIT地址:http://git.oschina.net/brucekankan/
點擊這里快速進入GIT
總結
以上是生活随笔為你收集整理的Spring mvc ContextLoaderListener 原理解析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java 动态代理 原理解析
- 下一篇: Spring bean 标签加载、解析过