Spring中ContextLoaderListener監(jiān)聽詳解
前言
web.xml配置文件的部分,方便下面的理解:
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:Resource/SpringConf.xml</param-value> </context-param> <servlet> <servlet-name>springweb</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/springweb.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springweb</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
一、SpringMVC總覽
SpringMVC啟動時會啟動WebApplicationContext類型的容器,并且會調(diào)用之前分析的refresh方法。
還是從原始的SpringMVC開始,啟動方式有三種,配置文件中配置ContextLoadServlet,ContextLoaderListener、ContextLoaderPlugin。
不論哪種方式啟動都大致相同。主要包括兩部分:
1、ContextLoaderListener監(jiān)聽
初始化Spring容器,WebApplicationContext類型,并且執(zhí)行refresh方法
2、DispatcherServlet(一個特殊的Servlet,如上面的配置web.xml中進行配置了)
1)、DispatcherServlet像普通Servlet一樣,init中啟動九大件為服務請求做準備
HttpServletBean#init => DispatcherServlet#onRefresh => DispatcherServlet#initStrategies(初始化九大件)
2)、當上面初始化完成后,則請求進入后攔截轉(zhuǎn)發(fā)到DispatcherServlet
DispatcherServlet#doService=> DispatcherServlet#doDispatch(執(zhí)行MVC流程)
二、ContextLoaderListener的contextInitialized監(jiān)聽事件
當Web容器啟動時,觸發(fā)ContextLoaderListener的contextInitialized方法:
@Override public void contextInitialized(ServletContextEvent event) { initWebApplicationContext(event.getServletContext()); }
public WebApplicationContext initWebApplicationContext(ServletContext servletContext) { // 省略部分日志和try catch代碼 long startTime = System.currentTimeMillis(); if (this.context == null) { this.context = createWebApplicationContext(servletContext); } if (this.context instanceof ConfigurableWebApplicationContext) { ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext) this.context; if (!cwac.isActive()) { // The context has not yet been refreshed -> provide services such as // setting the parent context, setting the application context id, etc if (cwac.getParent() == null) { // The context instance was injected without an explicit parent -> // determine parent for root web application context, if any. ApplicationContext parent = loadParentContext(servletContext); cwac.setParent(parent); } configureAndRefreshWebApplicationContext(cwac, servletContext); } } servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context); ClassLoader ccl = Thread.currentThread().getContextClassLoader(); if (ccl == ContextLoader.class.getClassLoader()) { currentContext = this.context; } else if (ccl != null) { currentContextPerThread.put(ccl, this.context); } return this.context; }
這里需要考慮容器是否其他地方已經(jīng)創(chuàng)建了,但是不論是否創(chuàng)建。整個Spring MVC啟動過程需要經(jīng)歷幾個步驟:
1、createWebApplicationContext(創(chuàng)建Web類型的ApplicationContext)
2、configureAndRefreshWebApplicationContext(主要是refresh方法執(zhí)行)
3、將初始化的WebApplicationContext容器,用key為ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE加載到ServletContext容器中
創(chuàng)建Web類型的ApplicationContext
protected WebApplicationContext createWebApplicationContext(ServletContext sc) { Class<?> contextClass = determineContextClass(sc); if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) { throw new ApplicationContextException("省略"); } return (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass); }
先判斷啟動的WebApplicationContext的類型Class,再使用反射初始化。主要是看啟動的Class是怎么確定的。
protected Class<?> determineContextClass(ServletContext servletContext) { String contextClassName = servletContext.getInitParameter(CONTEXT_CLASS_PARAM); if (contextClassName != null) { // 省略try catch代碼 return ClassUtils.forName(contextClassName, ClassUtils.getDefaultClassLoader()); } else { contextClassName = defaultStrategies.getProperty(WebApplicationContext.class.getName()); // 省略try catch代碼 return ClassUtils.forName(contextClassName, ContextLoader.class.getClassLoader()); } }
先看啟動Web時是否有進行配置Class,否則就在默認的配置策略中進行獲取。詳細可以參見:Spring中的策略模式簡單實現(xiàn)與使用分析
從配置文件ContextLoader.properties中獲取到的類型為(所以如果沒有配置默認啟動XmlWebApplicationContext,Spring Boot不會啟動該類型,但是SpringMVC沒有太大差異):
org.springframework.web.context.WebApplicationContext=org.springframework.web.context.support.XmlWebApplicationContext
執(zhí)行refresh方法
不管怎么樣都回執(zhí)行configureAndRefreshWebApplicationContext方法,以啟動Spring 容器,也就是之前一直分析refresh方法。
protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac, ServletContext sc) { // 設(shè)置ConfigurableWebApplicationContext的容器Id if (ObjectUtils.identityToString(wac).equals(wac.getId())) { // The application context id is still set to its original default value // -> assign a more useful id based on available information String idParam = sc.getInitParameter(CONTEXT_ID_PARAM); if (idParam != null) { wac.setId(idParam); } else { // Generate default id... wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + ObjectUtils.getDisplayString(sc.getContextPath())); } } // 設(shè)置Web容器上下文,設(shè)置到Spring容器中 wac.setServletContext(sc); String configLocationParam = sc.getInitParameter(CONFIG_LOCATION_PARAM); // 配置location,如果配置的話,則refresh方法時會調(diào)用Resource加載,解析,初始化 if (configLocationParam != null) { wac.setConfigLocation(configLocationParam); } // 設(shè)置Environment,refresh時也分析過,如果沒有則會從java.lang.System中 // 獲取數(shù)據(jù)初始化StandardEnvironment或其子類 ConfigurableEnvironment env = wac.getEnvironment(); if (env instanceof ConfigurableWebEnvironment) { ((ConfigurableWebEnvironment) env).initPropertySources(sc, null); } // 初始化自定義的容器,如果自己配置的話(我們可以自定義實現(xiàn)ServletContextListener) customizeContext(sc, wac); // 調(diào)用refresh方法 wac.refresh(); }
1、設(shè)置WebApplicationContext的Id
2、設(shè)置Web容器上下文,設(shè)置到Spring容器中
3、配置location(比如上面配置中的contextConfigLocation,則refresh方法時會調(diào)用Resource加載,解析,初始化)
4、初始化自定義的監(jiān)聽
5、最重要的一步,啟動Spring容器(可以從這里開始看:SpringIoc源碼(五)- ApplicationContext(一)- 結(jié)構(gòu)梳理)
到此這篇關(guān)于Spring中ContextLoaderListener監(jiān)聽詳解的文章就介紹到這了,更多相關(guān)ContextLoaderListener監(jiān)聽內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot 使用 FTP 操作文件的過程(刪除、上傳、下載文件)
這篇文章主要介紹了SpringBoot 使用 FTP 操作文件,主要包括配置ftp服務器,上傳、刪除、下載文件操作,本文結(jié)合示例代碼給大家介紹的非常詳細,需要的朋友可以參考下2022-12-12- 下面小編就為大家?guī)硪黄狫ava創(chuàng)建數(shù)組的幾種方式總結(jié)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧,希望能給大家?guī)韼椭?/div> 2021-06-06
IDEA最新激活碼2021(IDEA2020.3.2最新永久激活方法)
這篇文章主要介紹了IDEA最新激活碼2021(IDEA2020.3.2最新永久激活方法),本文通過實例圖文相結(jié)合給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12最新評論