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

