ContextLoader和ContextLoaderListener
Posted 恒奇恒毅
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ContextLoader和ContextLoaderListener相关的知识,希望对你有一定的参考价值。
每一个整合spring框架的项目中,总是不可避免地要在web.xml中加入这样一段配置。
<!-- Spring配置文件开始 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring配置文件结束 -->
这段配置由于使用Servlet
容器声明了一个ServletContextListener
,那么下面我们来看看其运行原理。
一、ContextLoaderListener
/**
* Bootstrap listener to start up and shut down Spring's root @link WebApplicationContext.
* Simply delegates to @link ContextLoader as well as to @link ContextCleanupListener.
*
* <p>This listener should be registered after @link org.springframework.web.util.Log4jConfigListener
* in @code web.xml, if the latter is used.
*
* <p>As of Spring 3.1, @code ContextLoaderListener supports injecting the root web
* application context via the @link #ContextLoaderListener(WebApplicationContext)
* constructor, allowing for programmatic configuration in Servlet 3.0+ environments.
* See @link org.springframework.web.WebApplicationInitializer for usage examples.
*
* @author Juergen Hoeller
* @author Chris Beams
* @since 17.02.2003
* @see #setContextInitializers
* @see org.springframework.web.WebApplicationInitializer
* @see org.springframework.web.util.Log4jConfigListener
*/
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 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
继承自ContextLoader
,并且实现了ServletContextListener
,实现ContextLoader
就可以调用父类写好的方法,而实现ServletContextListener
就可以在Servlet
容器启动和销毁的时候调用contextInitialized
和contextDestroyed
方法。
实现ServletContextListener有什么作用?
ServletContextListener
接口里的函数会结合Web容器的生命周期被调用。因为ServletContextListener
是ServletContext
的监听者,如果ServletContext
发生变化,会触发相应的事件,而监听器一直对事件监听,如果接收到了变化,就会做出预先设计好的相应动作。由于ServletContext
变化而触发的监听器的响应具体包括:在服务器启动时,ServletContext
被创建的时候,服务器关闭时,ServletContext
将被销毁的时候等。
ContextLoaderListener的作用是什么?
ContextLoaderListener
的作用就是启动Web容器时,读取在contextConfigLocation
中定义的xml文件,自动装配ApplicationContext的配置信息,并产生WebApplicationContext对象,然后将这个对象放置在ServletContext
的属性里,这样我们只要得到Servlet就可以得到WebApplicationContext对象,并利用这个对象访问spring容器管理的bean。
简单来说,就是上面这段配置为项目提供了spring支持,初始化了Ioc容器。
构造方法
提供了两个构造方法,一个无参和一个有参(传入了WebApplicationContext
)构造器
public ContextLoader()
public ContextLoader(WebApplicationContext context)
this.context = context;
无参构造器一般用于Servlet
容器反射调用,有参构造器一般用于在其他地方直接调用,可以传入一个WebApplicationContext
ServletContextListener#contextInitialized
该方法中调用了父类ContextLoader#initWebApplicationContext
方法,用于完成容器的初始化
ServletContextListener#contextDestroyed
该方法中做了两件事情:关闭容器,清除ServletContext
域中的属性。
关闭容器方法我们在ContextLoader类中再分析。清除属性只是清除了部分属性名以org.springframework.
开头的并且实现了DisposableBean
接口的对象(调用其destroy
方法)。
static void cleanupAttributes(ServletContext sc)
Enumeration<String> attrNames = sc.getAttributeNames();
while (attrNames.hasMoreElements())
String attrName = attrNames.nextElement();
if (attrName.startsWith("org.springframework."))
Object attrValue = sc.getAttribute(attrName);
if (attrValue instanceof DisposableBean)
try
((DisposableBean) attrValue).destroy();
catch (Throwable ex)
logger.error("Couldn't invoke destroy method of attribute with name '" + attrName + "'", ex);
二、ContextLoader
ContextLoader
提供了真正初始化和关闭容器的方法。
initWebApplicationContext
public WebApplicationContext initWebApplicationContext(ServletContext 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!");
Log logger = LogFactory.getLog(ContextLoader.class);
servletContext.log("Initializing Spring root WebApplicationContext");
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)
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);
if (logger.isDebugEnabled())
logger.debug("Published root WebApplicationContext as ServletContext attribute with name [" +
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE + "]");
if (logger.isInfoEnabled())
long elapsedTime = System.currentTimeMillis() - startTime;
logger.info("Root WebApplicationContext: initialization completed in " + elapsedTime + " ms");
return this.context;
catch (RuntimeException ex)
logger.error("Context initialization failed", ex);
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ex);
throw ex;
catch (Error err)
logger.error("Context initialization failed", err);
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, err);
throw err;
关键的几个步骤
- 判断context是否为空,不为空就直接用,为空就创建createWebApplicationContext
- 如果其父容器为空,则找一找是否存在父容器loadParentContext,并设置
- 配置并刷新configureAndRefreshWebApplicationContext
- 往ServletContext以
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE
放入容器,方便以后直接从ServletContext
中获取,即WebApplicationContextUtils#getWebApplicationContext(ServletContext)
- 如果当前线程的类加载器就是ContextLoader的累加器,则设置currentContext为当期容器;否则以当前类加载器为key,当前容器为value放入一个map中currentContextPerThread
currentContext和currentContextPerThread共同完成可以根据ClassLoader
获取相关容器的功能
createWebApplicationContext
protected WebApplicationContext createWebApplicationContext(ServletContext sc)
Class<?> contextClass = determineContextClass(sc);
if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass))
throw new ApplicationContextException("Custom context class [" + contextClass.getName() +
"] is not of type [" + ConfigurableWebApplicationContext.class.getName() + "]");
return (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);
protected Class<?> determineContextClass(ServletContext servletContext)
String contextClassName = servletContext.getInitParameter(CONTEXT_CLASS_PARAM);
if (contextClassName != null)
try
return ClassUtils.forName(contextClassName, ClassUtils.getDefaultClassLoader());
catch (ClassNotFoundException ex)
throw new ApplicationContextException(
"Failed to load custom context class [" + contextClassName + "]", ex);
else
contextClassName = defaultStrategies.getProperty(WebApplicationContext.class.getName());
try
return ClassUtils.forName(contextClassName, ContextLoader.class.getClassLoader());
catch (ClassNotFoundException ex)
throw new ApplicationContextException(
"Failed to load default context class [" + contextClassName + "]", ex);
- 首先在servlet的初始化参数中获取contextClass,获取到了就初始化
- 第一步没配置则走默认策略,
ContextLoader.properties
中配置的org.springframework.web.context.WebApplicationContext=org.springframework.web.context.support.XmlWebApplicationContext
,说明如果我们不配置contextClass,那么就默认使用XmlWebApplicationContext
loadParentContext
protected ApplicationContext loadParentContext(ServletContext servletContext)
ApplicationContext parentContext = null;
String locatorFactorySelector = servletContext.getInitParameter(LOCATOR_FACTORY_SELECTOR_PARAM);
String parentContextKey = servletContext.getInitParameter(LOCATOR_FACTORY_KEY_PARAM);
if (parentContextKey != null)
// locatorFactorySelector may be null, indicating the default "classpath*:beanRefContext.xml"
BeanFactoryLocator locator = ContextSingletonBeanFactoryLocator.getInstance(locatorFactorySelector);
Log logger = LogFactory.getLog(ContextLoader.class);
if (logger.isDebugEnabled())
logger.debug("Getting parent context definition: using parent context key of '" +
parentContextKey + "' with BeanFactoryLocator");
this.parentContextRef = locator.useBeanFactory(parentContextKey);
parentContext = (ApplicationContext) this.parentContextRef.getFactory();
return parentContext;
这个用的比较少,就不说了
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);
wac.refresh();
- 首先判断id如果还是默认的id,即ObjectUtils.identityToString(wac),则设置id。因为在中默认id就是
AbstractApplicationContext#id=ObjectUtils.identityToString(this);
- id的获取首先从
ServletContext
的初始化参数中获取,获取到则使用,没获取到则产生一个默认的 - 为容器设置
ServletContext
- 从
ServletContext
的初始化参数中获取配置文件contextConfigLocation位置,不为空则设置
支持的格式WEB-INF/applicationContext1.xml,WEB-INF/applicationContext2.xml,也支持Ant样式的路径模式,例如:WEB-INF/Context.xml、WEB-INF/spring.xml或WEB-INF/**/*Context.xml”。如果没有明确指定,上下文实现应该使用默认位置(使用XmlWebApplicationContext:“/WEB-INF/applicationContext.xml - 初始化属性设置
- 自定义容器配置customizeContext
- 刷新容器
customizeContext
protected void customizeContext(ServletContext sc, ConfigurableWebApplicationContext wac)
List<Class<ApplicationContextInitializer<ConfigurableApplicationContext>>> initializerClasses =
determineContextInitializerClasses(sc);
for (Class<ApplicationContextInitializer<ConfigurableApplicationContext>> initializerClass : initializerClasses)
Class<?> initializerContextClass =
GenericTypeResolver.resolveTypeArgument(initializerClass, ApplicationContextInitializer.class);
if (initializerContextClass != null && !initializerContextClass.isInstance(wac))
throw new ApplicationContextException(String.format(
"Could not apply context initializer [%s] since its generic parameter [%s] " +
"is not assignable from the type of application context used by this " +
"context loader: [%s]", initializerClass.getName(), initializerContextClass.getName(),
wac.getClass().getName()));
this.contextInitializers.add(BeanUtils.instantiateClass(initializerClass));
AnnotationAwareOrderComparator.sort(this.contextInitializers);
for (ApplicationContextInitializer<ConfigurableApplicationContext> initializer : this.contextInitializers)
initializer.initialize(wac);
protected List<Class<ApplicationContextInitializer<ConfigurableApplicationContext>>>
determineContextInitializerClasses(ServletContext servletContext)
List<Class<ApplicationContextInitializer<ConfigurableApplicationContext>>> classes =
new ArrayList<Class<ApplicationContextInitializer<ConfigurableApplicationContext>>>();
String globalClassNames = servletContext.getInitParameter(GLOBAL_INITIALIZER_CLASSES_PARAM);
if (globalClassNames != null)
for (String className : StringUtils.tokenizeToStringArray(globalClassNames, INIT_PARAM_DELIMITERS)以上是关于ContextLoader和ContextLoaderListener的主要内容,如果未能解决你的问题,请参考以下文章
ContextLoader和ContextLoaderListener
ContextLoader和ContextLoaderListener
ContextLoader,ContextLoaderListener解读
idea出现Error configuring application listener of class org.springframework.web.context.ContextLoader(
启动tomcat服务器报错:ERROR ContextLoader:307 - Context initialization failed