ITeye博客

《Spring技术内幕》学习笔记10——Web环境中Spring的启动过程

wang4674890 · 2012-07-30 09:04 · java · spring · 编程语言 · Spring · Markdown

1.Spring 不但可以在 JavaSE 环境中应用,在 Web 环境中也可以广泛应用, Spring 在 web 环境中应用时,需要在应用的 web.xml 文件中添加如下的配置 [code="java"] …… contextConfigLocation /WEB-INF/applicationContext.xml org.springframework.web.context.ContextLoaderListener …… [/code] 通过 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 的启动和销毁过程,其源码如下: [code="java"] 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()); } } [/code] 通过对 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 上下文的主要源码如下: [code="java"] //初始化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); } } } [/code] 通过上面 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 上下文的主要源码: [code="java"] //关闭指定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(); } } } [/code] 4.WebApplicationContext 接口 : WebApplicationContext 是定义了 Spring Web 应用上下文的规范的接口, Web 服务器在启动时通过该接口来启动 Spring ,源码如下: [code="java"] 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(); } [/code] 5.XmlWebApplicationContext 源码: 在 3.(1)ContextLoader 初始化 Spring Web 上下文的 determineContextClass 方法中,我们知道 Spring 首先通过 Servlet 上下文从 web.xml 文件中获取用户自定义配置的 contextClass 参数值,如果没有获取到,则默认使用 Spring 的 XmlWebApplicationContext 作为 Spring Web 应用的 IoC 容器, XmlWebApplicationContext 是 WebApplicationContext 的实现类 ConfigurableWebApplicationContext 的子类,其源码如下: [code="java"] 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}; } } } [/code] 在 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 的工作流程分析完毕。

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。