<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 在web环境中通过ContextLoaderListener监听器启动,通过加载指定目录下的spring配置文件,Web服务器启动spring的初始化过程,如果没有指定,spring会默认加载
/WEB-INF 下的applicationContext.xml.
ContextLoaderListener实现了ServletContextListener接口,当ContextLoaderListener监听器启动时调用contextInitialized方法初始化spring的web应用上下文。
/**
* Initialize the root web application context.
*/
@Override
public void contextInitialized(ServletContextEvent event) {
//初始化web应用上下文
initWebApplicationContext(event.getServletContext());
}
contextLoaderListerner初始化web应用上下文源码如下:
public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {
判断在Servlet上下文中Spring Web应用给根上下文是否已经存在
if (servletContext.getAttribute(WebApplicationContext.
ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) {
throw new IllegalStateException(
"Cannot initialize context because there is already a root " +
"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()) { //是否是info级别日志, 提升性能
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); //创建web应用上下文
}
if (this.context instanceof ConfigurableWebApplicationContext) {
ConfigurableWebApplicationContext cwa=(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();
//如果当前线程的类加载器是contextLoader,则将当前上下文设置为创建的web应用上下文
if (ccl == ContextLoader.class.getClassLoader()) {
currentContext = this.context;
}
//如果当前线程的容器类加载器不为null,则将创建的web应用上下文和其类
//加载器缓存在容器类加载器—>Web应用上下文Map集合中
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);
.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;
}
}