1.Spring不但可以在JavaSE环境中应用,在Web环境中也可以广泛应用,Spring在web环境中应用时,需要在应用的web.xml文件中添加如下的配置:

……
<context-param>
	<param-name>contextConfigLocation</param-name>
	<!--Spring配置文件位置-->
	<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
	<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
<listener>
……

通过web描述文件我们可以看到,Spring在Web环境中通过ContextLoaderListener监听器类启动,通过加载”/WEB-INF/”目录下的Spring配置文件,Web服务器启动Spring的初始化过程。

ContextLoaderListener是一个监听器,和Web服务器的生命周期事件紧密关联,即当Web服务器启动时,Web服务器声明周期事件将会触发ContextLoaderListener监听器加载Spring配置文件,开始Spring在web服务器中的初始化工作。

2.ContextLoaderListener启动Spring:

ContextLoaderListener继承Spring的ContextLoader上下文加载器类,同时实现ServletContextListener接口(Servlet上下文监听器),监听Web服务器上下文的启动和停止事件,管理Web环境中Spring的启动和销毁过程,其源码如下:

public class ContextLoaderListener extends ContextLoader implements ServletContextListener {
	//Spring上下文加载器
	private ContextLoader contextLoader;
//初始化Spring根上下文,event参数是当前Web应用上下文事件
	public void contextInitialized(ServletContextEvent event) {
		//创建Spring上下文加载器,ContextLoaderListener继承ContextLoader,
		//所以ContextLoaderListener本身也是Spring的上下文加载器
		this.contextLoader = createContextLoader();
		if (this.contextLoader == null) {
			this.contextLoader = this;
		}
	    //根据给定的ServletContext上下文,初始化Spring Web应用上下文	this.contextLoader.initWebApplicationContext(event.getServletContext());
	}
	//创建Spring上下文加载器
	protected ContextLoader createContextLoader() {
		return null;
	}
	//获取Spring上下文加载器
	public ContextLoader getContextLoader() {
		return this.contextLoader;
	}
//销毁Spring根上下文
	public void contextDestroyed(ServletContextEvent event) {
		if (this.contextLoader != null) {
		//关闭指定Servlet的Spring Web根上下文	this.contextLoader.closeWebApplicationContext(event.getServletContext());
		}
	//清除Servlet上下文中被配置的属性	ContextCleanupListener.cleanupAttributes(event.getServletContext());
	}
}

通过对ContextLoaderListener的源码分析,我们看到ContextLoaderListener继承ContextLoader,所以ContextLoaderListener本身也是Spring的上下文加载器。

ContextLoaderListener实现了ServletContextListener接口,当Web应用在Web服务器中被被启动和停止时,Web服务器启动和停止事件会分别触发ContextLoaderListener的contextInitialized和contextDestroyed方法来初始化和销毁Spring上下文。我们通过上述对ContextLoaderListener的源码分析看到真正实现Spring上下文的初始化和销毁功能的是ContextLoader类,接下来我们分析ContextLoader初始化和销毁Spring Web上下文的过程。

3.ContextLoader初始化和销毁Spring Web上下文:

(1).ContextLoader初始化Spring Web上下文的主要源码如下:

//初始化Spring根上下文
public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {
//ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE = WebApplicationContext.class.getName() + ".ROOT";
//判断在Servlet上下文中Spring Web应用给根上下文是否已经存在
		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!");
		}
		//记录日志
		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 {
			//如果当前Web根容器有父容器,则获取父容器
			ApplicationContext parent = loadParentContext(servletContext);
			//根据给定Servlet容器和父容器创建Web应用容器,并且所创建的Web应用
//容器实例对象存储在本地变量中,以确保当Servlet容器关闭时该容器可用
			this.context = createWebApplicationContext(servletContext, parent);
		//将创建的Web应用上下文设置到Servlet上下文的应用根容器属性中	servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
			//获取当前线程的容器类加载器
			ClassLoader ccl = Thread.currentThread().getContextClassLoader();
			//如果当前线程的容器类加载器是ContextLoader类,则当前上下文就设置为
//创建的Web应用上下文
			if (ccl == ContextLoader.class.getClassLoader()) {
				currentContext = this.context;
			}
			//如果当前线程的容器类加载器不为null,则将创建的web应用上下文和其类
//加载器缓存在容器类加载器—>Web应用上下文Map集合中
			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");
			}
			//返回创建的Web应用上下文
			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;
		}
	}
//创建Web应用上下文
protected WebApplicationContext createWebApplicationContext(ServletContext sc, ApplicationContext parent) {
		//获取当前Servlet上下文接口的实现类
		Class<?> contextClass = determineContextClass(sc);
	//判断使用什么样的的类在Web容器中作为IoC容器
		if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) {
			throw new ApplicationContextException("Custom context class [" + contextClass.getName() +
					"] is not of type [" + ConfigurableWebApplicationContext.class.getName() + "]");
		}
	//Servlet上下文实现类是ConfigurableWebApplicationContext类型,
//使用JDK反射机制,调用Servlet上下文实现类的无参构造方法创建实例对象
		ConfigurableWebApplicationContext wac =
				(ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);
		//如果Servlet大版本是2,并且小版本号小于5,即是Servlet2.4以下标准
		if (sc.getMajorVersion() == 2 && sc.getMinorVersion() < 5) {
			//获取Servlet上下文名称
			String servletContextName = sc.getServletContextName();
		//为创建的Web应用上下文设置唯一的Id标识	wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + ObjectUtils.getDisplayString(servletContextName));
		}
		//Servlet2.5标准
		else {
			try {
				//调用Servlet上下文对象中getContextPath()方法
				String contextPath = (String) ServletContext.class.getMethod("getContextPath").invoke(sc);
			//为Web上下文设置唯一Id标识	wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + ObjectUtils.getDisplayString(contextPath));
			}
			catch (Exception ex) {
				throw new IllegalStateException("Failed to invoke Servlet 2.5 getContextPath method", ex);
			}
		}
		//设置Web上下文的父级上下文
		wac.setParent(parent);
		//设置Web上下文的Servlet上下文
		wac.setServletContext(sc);
	//Servlet上下文从web.xml文件中获取配置的contextConfigLocation参数,并
//将其作为Spring Web上下文配置资源的值	wac.setConfigLocation(sc.getInitParameter(CONFIG_LOCATION_PARAM));
	//调用用户自定义的设置应用上下文方法
		customizeContext(sc, wac);
		//刷新Spring Web容器,启动载入Spring配置资源的过程
		wac.refresh();
		return wac;
	}
//根据给定的Servlet上下文,获取Spring Web上下文的实现类//XmlWebApplicationContext或者用户自定义的Spring Web应用上下文
protected Class<?> determineContextClass(ServletContext servletContext) {
		//Servlet上下文从web.xml中获取配置的contextClass参数值
		String contextClassName = servletContext.getInitParameter(CONTEXT_CLASS_PARAM);
		//如果web.xml中额外配置了contextClass参数值
		if (contextClassName != null) {
			try {
				//使用JDK反射机制,实例化指定名称的contextClass类对象
				return ClassUtils.forName(contextClassName, ClassUtils.getDefaultClassLoader());
			}
			catch (ClassNotFoundException ex) {
				throw new ApplicationContextException(
						"Failed to load custom context class [" + contextClassName + "]", ex);
			}
		}
		//如果web.xml中没有配置contextClass参数值
		else {
		//获取Spring中默认的Web应用上下文策略,即XmlWebApplicationContext
			contextClassName = defaultStrategies.getProperty(WebApplicationContext.class.getName());
			try {
				//使用JDK反射机制,创建Spring Web应用上下文默认策略类对象
				return ClassUtils.forName(contextClassName, ContextLoader.class.getClassLoader());
			}
			catch (ClassNotFoundException ex) {
				throw new ApplicationContextException(
						"Failed to load default context class [" + contextClassName + "]", ex);
			}
		}
	}

通过上面ContextLoader初始化Spring Web上下文的主要源码分析,我们看到当Web应用在Web服务器启动时,Servlet上下文通过web.xml文件获取所配置的contextConfigLocation、contextClass等参数,定位Spring配置文件资源,调用Spring Web容器的刷新的refresh()方法启动Web环境中Spring Ioc容器的初始化过程,refresh()方法启动Spring IoC容器的初始化过程我们在IoC容器源码分析中已经分析过,这里不再重述。

(2). ContextLoader关闭Spring Web上下文的主要源码:

//关闭指定Servlet上下文中的Spring Web应用上下文
public void closeWebApplicationContext(ServletContext servletContext) {
		servletContext.log("Closing Spring root WebApplicationContext");
		try {
		//Spring Web应用上下文的类型都是ConfigurableWebApplicationContext
			if (this.context instanceof ConfigurableWebApplicationContext) {
				//关闭Spring Web应用上下文,释放资源,销毁所有缓存的单态模式Bean
				((ConfigurableWebApplicationContext) this.context).close();
			}
		}
		finally {
			//获取当前线程的上下文类加载器
			ClassLoader ccl = Thread.currentThread().getContextClassLoader();
			//将当前上下文对象设置为null
			if (ccl == ContextLoader.class.getClassLoader()) {
				currentContext = null;
			}
			//移除容器类加载器—>Web应用上下文Map集合中中key为指定类加载器的项
			else if (ccl != null) {
				currentContextPerThread.remove(ccl);
			}
		//移除Servlet上下文中Spring Web根上下文属性	servletContext.removeAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
				//释放父容器引用
			if (this.parentContextRef != null) {
				this.parentContextRef.release();
			}
		}
	}

4.WebApplicationContext接口:

WebApplicationContext是定义了Spring Web应用上下文的规范的接口,Web服务器在启动时通过该接口来启动Spring,源码如下:

public interface WebApplicationContext extends ApplicationContext {
	//用于定义在Servlet上下文中存储Spring Web根上下文
	String ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE = WebApplicationContext.class.getName() + ".ROOT";
	//请求范围
	String SCOPE_REQUEST = "request";
	//会话范围
	String SCOPE_SESSION = "session";
	//全局会话范围
	String SCOPE_GLOBAL_SESSION = "globalSession";
	//应用程序范围
	String SCOPE_APPLICATION = "application";
	//容器中存储的Servlet上下文环境的Bean名称
	String SERVLET_CONTEXT_BEAN_NAME = "servletContext";
	//web.xml文件中的配置Spring初始化参数的属性
	String CONTEXT_PARAMETERS_BEAN_NAME = "contextParameters";
	//Servlet上下文属性
	String CONTEXT_ATTRIBUTES_BEAN_NAME = "contextAttributes";
//获取当前应用程序所以Web容器的标准Servlet上下文
	ServletContext getServletContext();
}

5.XmlWebApplicationContext源码:

在3.(1)ContextLoader初始化Spring Web上下文的determineContextClass方法中,我们知道Spring首先通过Servlet上下文从web.xml文件中获取用户自定义配置的contextClass参数值,如果没有获取到,则默认使用Spring的XmlWebApplicationContext作为Spring Web应用的IoC容器,XmlWebApplicationContext是WebApplicationContext的实现类ConfigurableWebApplicationContext的子类,其源码如下:

public class XmlWebApplicationContext extends AbstractRefreshableWebApplicationContext {
	//Web应用中Spring配置文件的默认位置和名称,如果没有特别指定,则Spring会根据
	//此位置定义Spring Bean定义资源
	public static final String DEFAULT_CONFIG_LOCATION = "/WEB-INF/applicationContext.xml";
	//Spring Bean定义资源默认前缀
	public static final String DEFAULT_CONFIG_LOCATION_PREFIX = "/WEB-INF/";
	//Spring Bean定义资源默认后置
	public static final String DEFAULT_CONFIG_LOCATION_SUFFIX = ".xml";
	//在分析Spring IoC初始化过程中我们已经分析过,加载Spring Bean定义资源的方法,
	//通过Spring容器刷新的refresh()方法触发
	protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {
		//为Spring容器创建XML Bean定义读取器,加载Spring Bean定义资源
		XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
//设置Bean定义读取器,因为XmlWebApplicationContext是//DefaultResourceLoader的子类,所以使用默认资源加载器来定义Bean定义资源
		beanDefinitionReader.setResourceLoader(this);
		//为Bean定义读取器设置SAX实体解析器
		beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));
	//在加载Bean定义之前,调用子类提供的一些用户自定义初始化Bean定义读取器的方法
		initBeanDefinitionReader(beanDefinitionReader);
		//使用Bean定义读取器加载Bean定义资源
		loadBeanDefinitions(beanDefinitionReader);
	}
	//用户自定义初始化Bean定义读取器的方法
	protected void initBeanDefinitionReader(XmlBeanDefinitionReader beanDefinitionReader) {
	}
	//加载Bean定义资源
	protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws IOException {
		//获取定位的Bean定义资源路径
		String[] configLocations = getConfigLocations();
		if (configLocations != null) {
			//遍历加载所有定义的Bean定义资源
			for (String configLocation : configLocations) {
				reader.loadBeanDefinitions(configLocation);
			}
		}
	}
	//获取默认Bean定义资源
	protected String[] getDefaultConfigLocations() {
//获取web.xml中的命名空间,如命名空间不为null,则返回 “/WEB-INF/命名空间.xml”
		if (getNamespace() != null) {
			return new String[] {DEFAULT_CONFIG_LOCATION_PREFIX + getNamespace() + DEFAULT_CONFIG_LOCATION_SUFFIX};
		}
		//如果命名空间为null,则返回"/WEB-INF/applicationContext.xml"
		else {
			return new String[] {DEFAULT_CONFIG_LOCATION};
		}
	}
}

在3.(1)ContextLoader初始化Spring Web上下文的createWebApplicationContext方法创建Web应用上下文时, 应用上下文对象通过setConfigLocation(sc.getInitParameter(CONFIG_LOCATION_PARAM));方法将web.xml中配置的contextConfigLocation参数(Spring配置文件位置)设置到Spring Web应用上下文中,在XmlWebApplicationContext的loadBeanDefinitions方法加载Spring Bean定义资源文件时,会逐个加载web.xml中配置的contextConfigLocation参数的Spring Bean定义资源文件。

XmlWebApplicationContext将Web应用中配置的Spring Bean定义资源文件载入到Spring IoC容器中后,接下来的Spring IoC容器初始化和依赖注入的过程我们已经在Spring IoC容器源码分析中具体分析过,至此Web环境中Spring的工作流程分析完毕。