zoukankan      html  css  js  c++  java
  • Web环境中Spring的启动过程

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

     

    1. ……  
    2. <context-param>  
    3.     <param-name>contextConfigLocation</param-name>  
    4.     <!--Spring配置文件位置-->  
    5.     <param-value>/WEB-INF/applicationContext.xml</param-value>  
    6. </context-param>  
    7. <listener>  
    8.     <listener-class>  
    9. org.springframework.web.context.ContextLoaderListener  
    10. </listener-class>  
    11. <listener>  
    12. ……  

    通过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的启动和销毁过程,其源码如下:

     

    1. public class ContextLoaderListener extends ContextLoader implements ServletContextListener {  
    2.     //Spring上下文加载器  
    3.     private ContextLoader contextLoader;  
    4. //初始化Spring根上下文,event参数是当前Web应用上下文事件  
    5.     public void contextInitialized(ServletContextEvent event) {  
    6.         //创建Spring上下文加载器,ContextLoaderListener继承ContextLoader,  
    7.         //所以ContextLoaderListener本身也是Spring的上下文加载器  
    8.         this.contextLoader = createContextLoader();  
    9.         if (this.contextLoader == null) {  
    10.             this.contextLoader = this;  
    11.         }  
    12.         //根据给定的ServletContext上下文,初始化Spring Web应用上下文 this.contextLoader.initWebApplicationContext(event.getServletContext());  
    13.     }  
    14.     //创建Spring上下文加载器  
    15.     protected ContextLoader createContextLoader() {  
    16.         return null;  
    17.     }  
    18.     //获取Spring上下文加载器  
    19.     public ContextLoader getContextLoader() {  
    20.         return this.contextLoader;  
    21.     }  
    22. //销毁Spring根上下文  
    23.     public void contextDestroyed(ServletContextEvent event) {  
    24.         if (this.contextLoader != null) {  
    25.         //关闭指定Servlet的Spring Web根上下文    this.contextLoader.closeWebApplicationContext(event.getServletContext());  
    26.         }  
    27.     //清除Servlet上下文中被配置的属性   ContextCleanupListener.cleanupAttributes(event.getServletContext());  
    28.     }  
    29. }  

    通过对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上下文的主要源码如下:

     

    1. //初始化Spring根上下文  
    2. public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {  
    3. //ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE = WebApplicationContext.class.getName() + ".ROOT";  
    4. //判断在Servlet上下文中Spring Web应用给根上下文是否已经存在  
    5.         if (servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE) != null) {  
    6.             throw new IllegalStateException(  
    7.                     "Cannot initialize context because there is already a root application context present - " +  
    8.                     "check whether you have multiple ContextLoader* definitions in your web.xml!");  
    9.         }  
    10.         //记录日志  
    11.         Log logger = LogFactory.getLog(ContextLoader.class);  
    12.         servletContext.log("Initializing Spring root WebApplicationContext");  
    13.         if (logger.isInfoEnabled()) {  
    14.             logger.info("Root WebApplicationContext: initialization started");  
    15.         }  
    16.         //获取当前系统时间  
    17.         long startTime = System.currentTimeMillis();  
    18.         try {  
    19.             //如果当前Web根容器有父容器,则获取父容器  
    20.             ApplicationContext parent = loadParentContext(servletContext);  
    21.             //根据给定Servlet容器和父容器创建Web应用容器,并且所创建的Web应用  
    22. //容器实例对象存储在本地变量中,以确保当Servlet容器关闭时该容器可用  
    23.             this.context = createWebApplicationContext(servletContext, parent);  
    24.         // 将创建的Web应用上下文设置到Servlet上下文的应用根容器属性 中    servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);  
    25.             //获取当前线程的容器类加载器  
    26.             ClassLoader ccl = Thread.currentThread().getContextClassLoader();  
    27.             //如果当前线程的容器类加载器是ContextLoader类,则当前上下文就设置为  
    28. //创建的Web应用上下文  
    29.             if (ccl == ContextLoader.class.getClassLoader()) {  
    30.                 currentContext = this.context;  
    31.             }  
    32.             //如果当前线程的容器类加载器不为null,则将创建的web应用上下文和其类  
    33. //加载器缓存在容器类加载器—>Web应用上下文Map集合中  
    34.             else if (ccl != null) {  
    35.                 currentContextPerThread.put(ccl, this.context);  
    36.             }  
    37.             if (logger.isDebugEnabled()) {  
    38.                 logger.debug("Published root WebApplicationContext as ServletContext attribute with name [" +  
    39.         WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE + "]");  
    40.             }  
    41.             if (logger.isInfoEnabled()) {  
    42.                 long elapsedTime = System.currentTimeMillis() - startTime;  
    43.                 logger.info("Root WebApplicationContext: initialization completed in " + elapsedTime + " ms");  
    44.             }  
    45.             //返回创建的Web应用上下文  
    46.             return this.context;  
    47.         }  
    48.         catch (RuntimeException ex) {  
    49.             logger.error("Context initialization failed", ex);  
    50.             servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ex);  
    51.             throw ex;  
    52.         }  
    53.         catch (Error err) {  
    54.             logger.error("Context initialization failed", err);  
    55.     servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, err);  
    56.             throw err;  
    57.         }  
    58.     }  
    59. //创建Web应用上下文  
    60. protected WebApplicationContext createWebApplicationContext(ServletContext sc, ApplicationContext parent) {  
    61.         //获取当前Servlet上下文接口的实现类  
    62.         Class<?> contextClass = determineContextClass(sc);  
    63.     //判断使用什么样的的类在Web容器中作为IoC容器  
    64.         if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) {  
    65.             throw new ApplicationContextException("Custom context class [" + contextClass.getName() +  
    66.                     "] is not of type [" + ConfigurableWebApplicationContext.class.getName() + "]");  
    67.         }  
    68.     //Servlet上下文实现类是ConfigurableWebApplicationContext类型,  
    69. //使用JDK反射机制,调用Servlet上下文实现类的无参构造方法创建实例对象  
    70.         ConfigurableWebApplicationContext wac =  
    71.                 (ConfigurableWebApplicationContext) BeanUtils.instantiateClass(contextClass);  
    72.         //如果Servlet大版本是2,并且小版本号小于5,即是Servlet2.4以下标准  
    73.         if (sc.getMajorVersion() == 2 && sc.getMinorVersion() < 5) {  
    74.             //获取Servlet上下文名称  
    75.             String servletContextName = sc.getServletContextName();  
    76.         // 为创建的Web应用上下文设置唯一的Id标 识 wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + ObjectUtils.getDisplayString(servletContextName));  
    77.         }  
    78.         //Servlet2.5标准  
    79.         else {  
    80.             try {  
    81.                 //调用Servlet上下文对象中getContextPath()方法  
    82.                 String contextPath = (String) ServletContext.class.getMethod("getContextPath").invoke(sc);  
    83.             // 为Web上下文设置唯一Id标 识   wac.setId(ConfigurableWebApplicationContext.APPLICATION_CONTEXT_ID_PREFIX + ObjectUtils.getDisplayString(contextPath));  
    84.             }  
    85.             catch (Exception ex) {  
    86.                 throw new IllegalStateException("Failed to invoke Servlet 2.5 getContextPath method", ex);  
    87.             }  
    88.         }  
    89.         //设置Web上下文的父级上下文  
    90.         wac.setParent(parent);  
    91.         //设置Web上下文的Servlet上下文  
    92.         wac.setServletContext(sc);  
    93.     //Servlet上下文从web.xml文件中获取配置的contextConfigLocation参数,并  
    94. //将其作为Spring Web上下文配置资源的值   wac.setConfigLocation(sc.getInitParameter(CONFIG_LOCATION_PARAM));  
    95.     //调用用户自定义的设置应用上下文方法  
    96.         customizeContext(sc, wac);  
    97.         //刷新Spring Web容器,启动载入Spring配置资源的过程  
    98.         wac.refresh();  
    99.         return wac;  
    100.     }  
    101. //根据给定的Servlet上下文,获取Spring Web上下文的实现类//XmlWebApplicationContext或者用户自定义的Spring Web应用上下文  
    102. protected Class<?> determineContextClass(ServletContext servletContext) {  
    103.         //Servlet上下文从web.xml中获取配置的contextClass参数值  
    104.         String contextClassName = servletContext.getInitParameter(CONTEXT_CLASS_PARAM);  
    105.         //如果web.xml中额外配置了contextClass参数值  
    106.         if (contextClassName != null) {  
    107.             try {  
    108.                 //使用JDK反射机制,实例化指定名称的contextClass类对象  
    109.                 return ClassUtils.forName(contextClassName, ClassUtils.getDefaultClassLoader());  
    110.             }  
    111.             catch (ClassNotFoundException ex) {  
    112.                 throw new ApplicationContextException(  
    113.                         "Failed to load custom context class [" + contextClassName + "]", ex);  
    114.             }  
    115.         }  
    116.         //如果web.xml中没有配置contextClass参数值  
    117.         else {  
    118.         //获取Spring中默认的Web应用上下文策略,即XmlWebApplicationContext  
    119.             contextClassName = defaultStrategies.getProperty(WebApplicationContext.class.getName());  
    120.             try {  
    121.                 //使用JDK反射机制,创建Spring Web应用上下文默认策略类对象  
    122.                 return ClassUtils.forName(contextClassName, ContextLoader.class.getClassLoader());  
    123.             }  
    124.             catch (ClassNotFoundException ex) {  
    125.                 throw new ApplicationContextException(  
    126.                         "Failed to load default context class [" + contextClassName + "]", ex);  
    127.             }  
    128.         }  
    129.     }  

    通过上面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上下文的主要源码:

     

    1. //关闭指定Servlet上下文中的Spring Web应用上下文  
    2. public void closeWebApplicationContext(ServletContext servletContext) {  
    3.         servletContext.log("Closing Spring root WebApplicationContext");  
    4.         try {  
    5.         //Spring Web应用上下文的类型都是ConfigurableWebApplicationContext  
    6.             if (this.context instanceof ConfigurableWebApplicationContext) {  
    7.                 //关闭Spring Web应用上下文,释放资源,销毁所有缓存的单态模式Bean  
    8.                 ((ConfigurableWebApplicationContext) this.context).close();  
    9.             }  
    10.         }  
    11.         finally {  
    12.             //获取当前线程的上下文类加载器  
    13.             ClassLoader ccl = Thread.currentThread().getContextClassLoader();  
    14.             //将当前上下文对象设置为null  
    15.             if (ccl == ContextLoader.class.getClassLoader()) {  
    16.                 currentContext = null;  
    17.             }  
    18.             //移除容器类加载器—>Web应用上下文Map集合中中key为指定类加载器的项  
    19.             else if (ccl != null) {  
    20.                 currentContextPerThread.remove(ccl);  
    21.             }  
    22.         //移除Servlet上下文中Spring Web根上下文属性 servletContext.removeAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);  
    23.                 //释放父容器引用  
    24.             if (this.parentContextRef != null) {  
    25.                 this.parentContextRef.release();  
    26.             }  
    27.         }  
    28.     }  

    4.WebApplicationContext接口:

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

     

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

    5.XmlWebApplicationContext源码:

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

    1. public class XmlWebApplicationContext extends AbstractRefreshableWebApplicationContext {  
    2.     //Web应用中Spring配置文件的默认位置和名称,如果没有特别指定,则Spring会根据  
    3.     //此位置定义Spring Bean定义资源  
    4.     public static final String DEFAULT_CONFIG_LOCATION = "/WEB-INF/applicationContext.xml";  
    5.     //Spring Bean定义资源默认前缀  
    6.     public static final String DEFAULT_CONFIG_LOCATION_PREFIX = "/WEB-INF/";  
    7.     //Spring Bean定义资源默认后置  
    8.     public static final String DEFAULT_CONFIG_LOCATION_SUFFIX = ".xml";  
    9.     //在分析Spring IoC初始化过程中我们已经分析过,加载Spring Bean定义资源的方法,  
    10.     //通过Spring容器刷新的refresh()方法触发  
    11.     protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {  
    12.         //为Spring容器创建XML Bean定义读取器,加载Spring Bean定义资源  
    13.         XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);  
    14. //设置Bean定义读取器,因为XmlWebApplicationContext是//DefaultResourceLoader的子类,所以使用默认资源加载器来定义Bean定义资源  
    15.         beanDefinitionReader.setResourceLoader(this);  
    16.         //为Bean定义读取器设置SAX实体解析器  
    17.         beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));  
    18.     //在加载Bean定义之前,调用子类提供的一些用户自定义初始化Bean定义读取器的方法  
    19.         initBeanDefinitionReader(beanDefinitionReader);  
    20.         //使用Bean定义读取器加载Bean定义资源  
    21.         loadBeanDefinitions(beanDefinitionReader);  
    22.     }  
    23.     //用户自定义初始化Bean定义读取器的方法  
    24.     protected void initBeanDefinitionReader(XmlBeanDefinitionReader beanDefinitionReader) {  
    25.     }  
    26.     //加载Bean定义资源  
    27.     protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws IOException {  
    28.         //获取定位的Bean定义资源路径  
    29.         String[] configLocations = getConfigLocations();  
    30.         if (configLocations != null) {  
    31.             //遍历加载所有定义的Bean定义资源  
    32.             for (String configLocation : configLocations) {  
    33.                 reader.loadBeanDefinitions(configLocation);  
    34.             }  
    35.         }  
    36.     }  
    37.     //获取默认Bean定义资源  
    38.     protected String[] getDefaultConfigLocations() {  
    39. //获取web.xml中的命名空间,如命名空间不为null,则返回 “/WEB-INF/命名空间.xml”  
    40.         if (getNamespace() != null) {  
    41.             return new String[] {DEFAULT_CONFIG_LOCATION_PREFIX + getNamespace() + DEFAULT_CONFIG_LOCATION_SUFFIX};  
    42.         }  
    43.         //如果命名空间为null,则返回"/WEB-INF/applicationContext.xml"  
    44.         else {  
    45.             return new String[] {DEFAULT_CONFIG_LOCATION};  
    46.         }  
    47.     }  
    48. }  

    在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的工作流程分析完毕。

  • 相关阅读:
    网站访问量大 怎样优化mysql数据库
    BootStrap 模态框禁用空白处点击关闭
    常用SQL语句
    诅咒JavaScript之----ArcGIS JavaScript 点聚合 ClusterLayer
    模态框与 天地图地图控件冲突
    FXK Javascript
    从列表中或数组中随机抽取固定数量的元素组成新的数组或列表
    wangEditor
    手把手教你用vue-cli构建一个简单的路由应用
    解决eclipse端口被占用的问题
  • 原文地址:https://www.cnblogs.com/zhaoxinshanwei/p/6560532.html
Copyright © 2011-2022 走看看