再进入createWebApplicationContext()
//ContextLoader
protected WebApplicationContext createWebApplicationContext(ServletContext sc) {
Class
contextClass = determineContextClass(sc); //返回WebApplicationContext实现类使用,默认XmlWebApplicationContext或如果指定一个自定义上下文类。
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);
}
再看determineConetxt()
//web.xmlcontextClass org.springframework.web.context.support.XmlWebApplicationContext
接着,我们回到initWebApplicationContext(),其中调用的configureAndRefreshWebApplicationContext(cwac, servletContext);我们看一下他的实现
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); //这里设置WebApplicationContext的id值为contextId也就是我们浏览器访问的localhost:8080/01/中的01
}
else {
// Generate default id...
wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX +
ObjectUtils.getDisplayString(sc.getContextPath()));
}
}
wac.setServletContext(sc);
String initParameter = sc.getInitParameter(CONFIG_LOCATION_PARAM);//这里设置Web.xml中配置的contextConfigLocation参数,也就是配置文件applicationContext.xml了
if (initParameter != null) {
wac.setConfigLocation(initParameter);
}
customizeContext(sc, wac);
wac.refresh(); //加载刚才的配置文件applicationContext.xml,以及bean的实例化
} 看一下web.xml中的contextConfigLocation设置吧
再看一下
//XmlWebApplicationContext
@Override
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
prepareRefresh();
// Tell t