在web.xml 中会配置一个listener监听器 这个监听器是来初始化webApplicationContext
当tomcat启动的时候会首先进入到这个监听器中 contextInitialized方法 调用了父类ContextLoader 中的initWebApplicationContext 方法
看看这个方法都干了什么
public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {
//首先进来先获取了一个servletContext的参数 暂时不知道这个参数的作用 tomacat 启动的时候 为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) {
//这里实例化一个webApplicationContext
// 在实例化的时候 首先会现从web.xml 中读取有没有配置contextClass 的参数
//如果没有的话 会从spring启动时候加载的配置文件中获取 WebApplicationContext.class.getName()
//再通过反射 ClassUtils.forName(contextClassName, ContextLoader.class.getClassLoader()) 返回一个class对象
// 最后通过 BeanUtils.instantiateClass(contextClass) 实例化这个webApplicationContext 其实是 ConfigurableWebApplicationContext这个类 是webApplicationContext的
//子类
}
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.
//这里应该是加载了beanFactory 在这次init的时候基本什么也没做就返回了parent
cwac.setParent(parent);
}
//这里获取了web.xml 中servletcontextPath 作为webApplicationContext 的id 和 获取ConfigLocation 给webApplicationContext 中set进去
}
}
//设置了一个值
//这个classLoader还没看明白
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;
}
}