Spring中的ContextLoaderListener詳細(xì)解析
前言
我們已經(jīng)了解到,在web容器即Tomact容器啟動web應(yīng)用即servlet應(yīng)用時,會觸發(fā)ServletContextEvent時間,這個事件會被ServletContextListener監(jiān)聽,監(jiān)聽到了以后會通過ContextLoaderListener監(jiān)聽器(ServletContextListener的一個實(shí)現(xiàn)類)去初始化Spring容器其實(shí)就是去初始化一些Bean。即默認(rèn)WEB-INF/applicationContext.xml文件的配置信息。
關(guān)于有如下兩點(diǎn)需要理解:
1.web項目自身:接收web容器啟動web應(yīng)用的通知,開始自身配置的解析加載,創(chuàng)建bean實(shí)例,通過一個WebApplicationContext來維護(hù)spring項目的主容器相關(guān)的bean,以及其他一些組件。
2.web容器:web容器使用ServletContext來維護(hù)每一個web應(yīng)用,ContextLoaderListener將spring容器,即WebApplicationContext,作為ServletContext的一個attribute,key為WebApplicationContext.class.getName() + “.ROOT”,保存在ServletContext中,從而web容器和spring項目可以通過ServletContext來交互。
ContextLoaderListener只是作為一個中間層來建立spring容器和web容器的關(guān)聯(lián)關(guān)系,而實(shí)際完成以上兩個角度的工作是通過ContextLoader來進(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類。
在web容器啟動的時候,ContextLoaderListener就會監(jiān)聽到。
并將監(jiān)聽事件傳入到方法:contextInitialized(ServletContextEvent event)中,然后,調(diào)用了父類的方法去初始化initWebApplicationContext:
Spring容器初始化
接下來我們的重點(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)建一個WebApplicationContext // 具體類型如果是指定了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對象創(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è)計層面主要定義了contextId,contextConfigLocation,contextClass,contextInitializerClasses。這些參數(shù)都可以在配置中指定,如web.xml的context-param標(biāo)簽,或者是基于Java編程方式配置的WebApplicationInitializer中定義,作為分別為:contextId:當(dāng)前容器的id,主要給底層所使用的BeanFactory,在進(jìn)行序列化時使用。
- contextConfigLocation:配置文件的位置,默認(rèn)為WEB-INF/applicationContext.xml,可以通過在web.xml使用context-param標(biāo)簽來指定其他位置,其他名字或者用逗號分隔指定多個。在配置文件中通過beans作為主標(biāo)簽來定義bean。這樣底層的BeanFactory會解析beans標(biāo)簽以及里面的bean,從而來創(chuàng)建BeanDefinitions集合,即bean的元數(shù)據(jù)內(nèi)存數(shù)據(jù)庫。
- contextClass:當(dāng)前所使用的WebApplicationContext的類型,如果是在WEB-INF/applicationContext.xml中指定beans,則使用XmlWebApplicationContext,如果是通過注解,如@Configuration,@Component等,則是AnnotationConfigWebApplicationContext,通過掃描basePackages指定的包來創(chuàng)建bean。
- contextInitializerClasses:ApplicationContextInitializer的實(shí)現(xiàn)類,即在調(diào)用ApplicationContext的refresh加載beanDefinition和創(chuàng)建bean之前,對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的啟動 // 即spring容器的各個核心組件的創(chuàng)建,如beanDefinitions,enviromnet等 wac.refresh(); }
總結(jié)
通過上面的分析可知,ContextLoader完成spring主容器的創(chuàng)建,工作主要是負(fù)責(zé)從ServletContext中,具體為ServletContext從web.xml文件或者WebApplicationInitializer的實(shí)現(xiàn)類中,獲取WebApplicationContext的相關(guān)配置信息,如使用contextClass指定使用哪種WebApplicationContext實(shí)現(xiàn),contextConfigLocation指定spring容器的配置文件在哪里,以及獲取WebApplicationContextInitializers來在進(jìn)行spring容器創(chuàng)建之前,對WebApplicationContext進(jìn)行加工處理。
而spring容器的創(chuàng)建,則是使用spring-context包的ApplicationContext,spring-beans包的BeanFactory,來完成從配置中獲取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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringApplicationRunListener監(jiān)聽器源碼詳解
- SpringBoot中的ApplicationListener事件監(jiān)聽器使用詳解
- Spring中的監(jiān)聽器SpringApplicationRunListener詳解
- Springboot使用@WebListener?作為web監(jiān)聽器的過程解析
- Spring事件監(jiān)聽器ApplicationListener源碼詳解
- SpringBoot ApplicationListener事件監(jiān)聽接口使用問題探究
- Spring事件監(jiān)聽器之@EventListener原理分析
相關(guān)文章
Java實(shí)現(xiàn)泡泡堂對戰(zhàn)版游戲的示例代碼
本文將利用Java制作經(jīng)典游戲《泡泡堂》,文中使用了MVC模式,分離了模型、視圖和控制器,使得項目結(jié)構(gòu)清晰易于擴(kuò)展,感興趣的可以了解一下2022-04-04spring Security的自定義用戶認(rèn)證過程詳解
這篇文章主要介紹了spring Security的自定義用戶認(rèn)證過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-09-09Java JDK動態(tài)代理(AOP)的實(shí)現(xiàn)原理與使用詳析
所謂代理,就是一個人或者一個機(jī)構(gòu)代表另一個人或者另一個機(jī)構(gòu)采取行動。下面這篇文章主要給大家介紹了關(guān)于Java JDK動態(tài)代理(AOP)實(shí)現(xiàn)原理與使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-07-07SpringCloud筆記(Hoxton)Netflix之Ribbon負(fù)載均衡示例代碼
這篇文章主要介紹了SpringCloud筆記HoxtonNetflix之Ribbon負(fù)載均衡,Ribbon是管理HTTP和TCP服務(wù)客戶端的負(fù)載均衡器,Ribbon具有一系列帶有名稱的客戶端(Named?Client),對SpringCloud?Ribbon負(fù)載均衡相關(guān)知識感興趣的朋友一起看看吧2022-06-06Druid連接池的自定義過濾功能實(shí)現(xiàn)方法
在數(shù)據(jù)密集型應(yīng)用中,監(jiān)控和分析數(shù)據(jù)庫操作對于確保性能和穩(wěn)定性至關(guān)重要,本文將探討如何實(shí)現(xiàn)一個自定義的Druid過濾器來捕獲數(shù)據(jù)庫請求并進(jìn)行日志記錄,以輔助開發(fā)和維護(hù)工作,需要的朋友可以參考下2023-11-11