ContextLoaderListener继承ServletContextListener类,实现contextInitialized方法
public void contextInitialized(ServletContextEvent event) {
this.contextLoader = createContextLoader();
this.contextLoader.initWebApplicationContext(event.getServletContext());
}
创建contextLoader,调用initWebApplicationContext方法:
// 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.
this.context = createWebApplicationContext(servletContext, parent);
//createWebApplicationContext方法还回默认的XmlWebApplicationContext
servletContext.setAttribute(
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
//并放入ServletContext中
跟踪到createWebApplicationContext(),
Class contextClass = determineContextClass(servletContext);
//Return the WebApplicationContext implementation class to use,
//either the default XmlWebApplicationContext or a custom context class if specified
if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) {
throw new ApplicationContextException("Custom context class [" + contextClass.getName() +
"] is not of type [" + ConfigurableWebApplicationContext.class.getName() + "]");
}
ConfigurableWebApplicationContext wac =
(ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);
// BeanUtils.instantiateClass is Convenience method to instantiate a class using its no-arg constructor
wac.setParent(parent);
wac.setServletContext(servletContext);
String configLocation = servletContext.getInitParameter(CONFIG_LOCATION_PARAM);
//configLocation = /WEB-INF/applicationContext.xml;
if (configLocation != null) {
wac.setConfigLocations(StringUtils.tokenizeToStringArray(configLocation,
ConfigurableWebApplicationContext.CONFIG_LOCATION_DELIMITERS));
//Set the config locations for this web application context.
//If not set, the implementation is supposed to use a default
//for the given namespace or the root web application context, as appropriate.
}
wac.refresh();
return wac;
最后返回XmlWebApplicationContext实例;