【IT168技术文档】
看一下是怎么来初始化webapplicationContext的
==>contextLoader.initWebApplicationContext
public WebApplicationContext initWebApplicationContext(ServletContext servletContext)
throws IllegalStateException, BeansException ...{
![]()
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!");
}
![]()
long startTime = System.currentTimeMillis();
if (logger.isInfoEnabled()) ...{
logger.info("Root WebApplicationContext: initialization started");
}
servletContext.log("Loading Spring root WebApplicationContext");
![]()
try ...{
// Determine parent for root web application context, if any.
ApplicationContext parent = loadParentContext(servletContext);
![]()
// Store context in local instance variable, to guarantee that
// it is available on ServletContext shutdown.
![]()
<span style="color:red;">
//创建web上下文
this.context = createWebApplicationContext(servletContext, parent);
servletContext.setAttribute(
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
</span>
![]()
if (logger.isInfoEnabled()) ...{
logger.info("Using context class [" + this.context.getClass().getName() +
"] for root WebApplicationContext");
}
if (logger.isDebugEnabled()) ...{
logger.debug("Published root WebApplicationContext [" + this.context +
"] 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;
}
}
