Spring中的ContextLoaderListener詳細(xì)解析
前言
我們已經(jīng)了解到,在web容器即Tomact容器啟動(dòng)web應(yīng)用即servlet應(yīng)用時(shí),會(huì)觸發(fā)ServletContextEvent時(shí)間,這個(gè)事件會(huì)被ServletContextListener監(jiān)聽(tīng),監(jiān)聽(tīng)到了以后會(huì)通過(guò)ContextLoaderListener監(jiān)聽(tīng)器(ServletContextListener的一個(gè)實(shí)現(xiàn)類(lèi))去初始化Spring容器其實(shí)就是去初始化一些Bean。即默認(rèn)WEB-INF/applicationContext.xml文件的配置信息。
關(guān)于有如下兩點(diǎn)需要理解:
1.web項(xiàng)目自身:接收web容器啟動(dòng)web應(yīng)用的通知,開(kāi)始自身配置的解析加載,創(chuàng)建bean實(shí)例,通過(guò)一個(gè)WebApplicationContext來(lái)維護(hù)spring項(xiàng)目的主容器相關(guān)的bean,以及其他一些組件。
2.web容器:web容器使用ServletContext來(lái)維護(hù)每一個(gè)web應(yīng)用,ContextLoaderListener將spring容器,即WebApplicationContext,作為ServletContext的一個(gè)attribute,key為WebApplicationContext.class.getName() + “.ROOT”,保存在ServletContext中,從而web容器和spring項(xiàng)目可以通過(guò)ServletContext來(lái)交互。
ContextLoaderListener只是作為一個(gè)中間層來(lái)建立spring容器和web容器的關(guān)聯(lián)關(guān)系,而實(shí)際完成以上兩個(gè)角度的工作是通過(guò)ContextLoader來(lái)進(jìn)行的,即在ContextLoader中定義以上邏輯,
ContextLoaderListener的實(shí)現(xiàn)
ContextLoaderListener的源碼如下:
public class ContextLoaderListener extends ContextLoader implements ServletContextListener { /** * Create a new {@code ContextLoaderListener} that will create a web application * context based on the "contextClass" and "contextConfigLocation" servlet * context-params. See {@link ContextLoader} superclass documentation for details on * default values for each. * <p>This constructor is typically used when declaring {@code ContextLoaderListener} * as a {@code <listener>} within {@code web.xml}, where a no-arg constructor is * required. * <p>The created application context will be registered into the ServletContext under * the attribute name {@link WebApplicationContext#ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE} * and the Spring application context will be closed when the {@link #contextDestroyed} * lifecycle method is invoked on this listener. * @see ContextLoader * @see #ContextLoaderListener(WebApplicationContext) * @see #contextInitialized(ServletContextEvent) * @see #contextDestroyed(ServletContextEvent) */ public ContextLoaderListener() { } /** * Create a new {@code ContextLoaderListener} with the given application context. This * constructor is useful in Servlet 3.0+ environments where instance-based * registration of listeners is possible through the {@link javax.servlet.ServletContext#addListener} * API. * <p>The context may or may not yet be {@linkplain * org.springframework.context.ConfigurableApplicationContext#refresh() refreshed}. If it * (a) is an implementation of {@link ConfigurableWebApplicationContext} and * (b) has <strong>not</strong> already been refreshed (the recommended approach), * then the following will occur: * <ul> * <li>If the given context has not already been assigned an {@linkplain * org.springframework.context.ConfigurableApplicationContext#setId id}, one will be assigned to it</li> * <li>{@code ServletContext} and {@code ServletConfig} objects will be delegated to * the application context</li> * <li>{@link #customizeContext} will be called</li> * <li>Any {@link org.springframework.context.ApplicationContextInitializer ApplicationContextInitializer org.springframework.context.ApplicationContextInitializer ApplicationContextInitializers} * specified through the "contextInitializerClasses" init-param will be applied.</li> * <li>{@link org.springframework.context.ConfigurableApplicationContext#refresh refresh()} will be called</li> * </ul> * If the context has already been refreshed or does not implement * {@code ConfigurableWebApplicationContext}, none of the above will occur under the * assumption that the user has performed these actions (or not) per his or her * specific needs. * <p>See {@link org.springframework.web.WebApplicationInitializer} for usage examples. * <p>In any case, the given application context will be registered into the * ServletContext under the attribute name {@link * WebApplicationContext#ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE} and the Spring * application context will be closed when the {@link #contextDestroyed} lifecycle * method is invoked on this listener. * @param context the application context to manage * @see #contextInitialized(ServletContextEvent) * @see #contextDestroyed(ServletContextEvent) */ public ContextLoaderListener(WebApplicationContext context) { super(context); } /** * Initialize the root web application context. */ @Override public void contextInitialized(ServletContextEvent event) { initWebApplicationContext(event.getServletContext()); } /** * Close the root web application context. */ @Override public void contextDestroyed(ServletContextEvent event) { closeWebApplicationContext(event.getServletContext()); ContextCleanupListener.cleanupAttributes(event.getServletContext()); }
可以看到ContextLoaderListener繼承了ServletContextListener并實(shí)現(xiàn)了ContextLoader類(lèi)。
在web容器啟動(dòng)的時(shí)候,ContextLoaderListener就會(huì)監(jiān)聽(tīng)到。
并將監(jiān)聽(tīng)事件傳入到方法:contextInitialized(ServletContextEvent event)中,然后,調(diào)用了父類(lèi)的方法去初始化initWebApplicationContext:
Spring容器初始化
接下來(lái)我們的重點(diǎn)是Spring容器是如何初始化的:
public WebApplicationContext initWebApplicationContext(ServletContext servletContext) { 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!"); } servletContext.log("Initializing Spring root WebApplicationContext"); Log logger = LogFactory.getLog(ContextLoader.class); if (logger.isInfoEnabled()) { logger.info("Root WebApplicationContext: initialization started"); } long startTime = System.currentTimeMillis(); try { // Store context in local instance variable, to guarantee that // it is available on ServletContext shutdown. if (this.context == null) { // 創(chuàng)建一個(gè)WebApplicationContext // 具體類(lèi)型如果是指定了contextClass則使用該指定的; // 默認(rèn)使用XmlWebApplicationContext 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); } // 核心方法,完成配置加載,BeanDefinition定義和bean對(duì)象創(chuàng)建 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); } if (logger.isInfoEnabled()) { long elapsedTime = System.currentTimeMillis() - startTime; logger.info("Root WebApplicationContext initialized in " + elapsedTime + " ms"); } return this.context; } catch (RuntimeException | Error ex) { logger.error("Context initialization failed", ex); servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ex); throw ex; } }
在看上述代碼前,我們需要了解ContextLoader的一些重要含義
ContextLoader主要負(fù)責(zé)加載spring主容器,即root ApplicationContext,在設(shè)計(jì)層面主要定義了contextId,contextConfigLocation,contextClass,contextInitializerClasses。這些參數(shù)都可以在配置中指定,如web.xml的context-param標(biāo)簽,或者是基于Java編程方式配置的WebApplicationInitializer中定義,作為分別為:contextId:當(dāng)前容器的id,主要給底層所使用的BeanFactory,在進(jìn)行序列化時(shí)使用。
- contextConfigLocation:配置文件的位置,默認(rèn)為WEB-INF/applicationContext.xml,可以通過(guò)在web.xml使用context-param標(biāo)簽來(lái)指定其他位置,其他名字或者用逗號(hào)分隔指定多個(gè)。在配置文件中通過(guò)beans作為主標(biāo)簽來(lái)定義bean。這樣底層的BeanFactory會(huì)解析beans標(biāo)簽以及里面的bean,從而來(lái)創(chuàng)建BeanDefinitions集合,即bean的元數(shù)據(jù)內(nèi)存數(shù)據(jù)庫(kù)。
- contextClass:當(dāng)前所使用的WebApplicationContext的類(lèi)型,如果是在WEB-INF/applicationContext.xml中指定beans,則使用XmlWebApplicationContext,如果是通過(guò)注解,如@Configuration,@Component等,則是AnnotationConfigWebApplicationContext,通過(guò)掃描basePackages指定的包來(lái)創(chuàng)建bean。
- contextInitializerClasses:ApplicationContextInitializer的實(shí)現(xiàn)類(lèi),即在調(diào)用ApplicationContext的refresh加載beanDefinition和創(chuàng)建bean之前,對(duì)WebApplicationContext進(jìn)行一些初始化
configureAndRefreshWebApplicationContext源碼如下:
protected void configureAndRefreshWebApplicationContext(ConfigurableWebApplicationContext wac, ServletContext sc) { 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())); } } wac.setServletContext(sc); String configLocationParam = sc.getInitParameter(CONFIG_LOCATION_PARAM); if (configLocationParam != null) { wac.setConfigLocation(configLocationParam); } // The wac environment's #initPropertySources will be called in any case when the context // is refreshed; do it eagerly here to ensure servlet property sources are in place for // use in any post-processing or initialization that occurs below prior to #refresh ConfigurableEnvironment env = wac.getEnvironment(); if (env instanceof ConfigurableWebEnvironment) { ((ConfigurableWebEnvironment) env).initPropertySources(sc, null); } customizeContext(sc, wac); // ApplicationContext的核心方法:在refresh方法中完成ApplicationContext的啟動(dòng) // 即spring容器的各個(gè)核心組件的創(chuàng)建,如beanDefinitions,enviromnet等 wac.refresh(); }
總結(jié)
通過(guò)上面的分析可知,ContextLoader完成spring主容器的創(chuàng)建,工作主要是負(fù)責(zé)從ServletContext中,具體為ServletContext從web.xml文件或者WebApplicationInitializer的實(shí)現(xiàn)類(lèi)中,獲取WebApplicationContext的相關(guān)配置信息,如使用contextClass指定使用哪種WebApplicationContext實(shí)現(xiàn),contextConfigLocation指定spring容器的配置文件在哪里,以及獲取WebApplicationContextInitializers來(lái)在進(jìn)行spring容器創(chuàng)建之前,對(duì)WebApplicationContext進(jìn)行加工處理。
而spring容器的創(chuàng)建,則是使用spring-context包的ApplicationContext,spring-beans包的BeanFactory,來(lái)完成從配置中獲取beans定義并創(chuàng)建BeanDefinition,獲取一些資源屬性值,以及完成單例bean的創(chuàng)建等。具體在之后關(guān)于ApplicationContext和BeanFactory體系結(jié)構(gòu)的文章中分析。
到此這篇關(guān)于Spring中的ContextLoaderListener詳細(xì)解析的文章就介紹到這了,更多相關(guān)Spring中的ContextLoaderListener內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringApplicationRunListener監(jiān)聽(tīng)器源碼詳解
- SpringBoot中的ApplicationListener事件監(jiān)聽(tīng)器使用詳解
- Spring中的監(jiān)聽(tīng)器SpringApplicationRunListener詳解
- Springboot使用@WebListener?作為web監(jiān)聽(tīng)器的過(guò)程解析
- Spring事件監(jiān)聽(tīng)器ApplicationListener源碼詳解
- SpringBoot ApplicationListener事件監(jiān)聽(tīng)接口使用問(wèn)題探究
- Spring事件監(jiān)聽(tīng)器之@EventListener原理分析
相關(guān)文章
Java實(shí)現(xiàn)泡泡堂對(duì)戰(zhàn)版游戲的示例代碼
本文將利用Java制作經(jīng)典游戲《泡泡堂》,文中使用了MVC模式,分離了模型、視圖和控制器,使得項(xiàng)目結(jié)構(gòu)清晰易于擴(kuò)展,感興趣的可以了解一下2022-04-04spring Security的自定義用戶認(rèn)證過(guò)程詳解
這篇文章主要介紹了spring Security的自定義用戶認(rèn)證過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09Java JDK動(dòng)態(tài)代理(AOP)的實(shí)現(xiàn)原理與使用詳析
所謂代理,就是一個(gè)人或者一個(gè)機(jī)構(gòu)代表另一個(gè)人或者另一個(gè)機(jī)構(gòu)采取行動(dòng)。下面這篇文章主要給大家介紹了關(guān)于Java JDK動(dòng)態(tài)代理(AOP)實(shí)現(xiàn)原理與使用的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-07-07SpringCloud筆記(Hoxton)Netflix之Ribbon負(fù)載均衡示例代碼
這篇文章主要介紹了SpringCloud筆記HoxtonNetflix之Ribbon負(fù)載均衡,Ribbon是管理HTTP和TCP服務(wù)客戶端的負(fù)載均衡器,Ribbon具有一系列帶有名稱的客戶端(Named?Client),對(duì)SpringCloud?Ribbon負(fù)載均衡相關(guān)知識(shí)感興趣的朋友一起看看吧2022-06-06Druid連接池的自定義過(guò)濾功能實(shí)現(xiàn)方法
在數(shù)據(jù)密集型應(yīng)用中,監(jiān)控和分析數(shù)據(jù)庫(kù)操作對(duì)于確保性能和穩(wěn)定性至關(guān)重要,本文將探討如何實(shí)現(xiàn)一個(gè)自定義的Druid過(guò)濾器來(lái)捕獲數(shù)據(jù)庫(kù)請(qǐng)求并進(jìn)行日志記錄,以輔助開(kāi)發(fā)和維護(hù)工作,需要的朋友可以參考下2023-11-11