Spring源码解读――Spring容器初始化 1(二)

2014-11-24 02:59:59 · 作者: · 浏览: 7
.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; } }
再进入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() width=700

可以看到在这里其实就是根据Parameter CONTEXT_CLASSPARAM去实例化一个WebApplicatoinContext,而该参数其实在web.xml已经配置为XmlWebApplicationContext(WebApplicationContext实现类) ,看web.xml
//web.xml

  
  
  
    
   
    contextClass
   
    
   
     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