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

Web环境中Spring的启动过程的更多相关文章

  1. web环境中的spring MVC

    1. web.xml文件的简单详解 在web环境中, spring MVC是建立在IOC容器的基础上,要了解spring mvc,首先要了解Spring IOC容器是如何在web环境中被载入并起作用的 ...

  2. ssm框架中,项目启动过程以及web.xml配置详解

    原文:https://blog.csdn.net/qq_35571554/article/details/82385838 本篇主要在基于SSM的框架,深入讲解web.xml的配置 web.xml   ...

  3. Spring IOC容器在Web容器中是怎样启动的

    前言 我们一般都知道怎样使用spring来开发web应用后,但对spring的内部实现机制通常不是很明白.这里从源码角度分析下Spring是怎样启动的.在讲spring启动之前,我们先来看看一个web ...

  4. Spring Boot启动过程(四):Spring Boot内嵌Tomcat启动

    之前在Spring Boot启动过程(二)提到过createEmbeddedServletContainer创建了内嵌的Servlet容器,我用的是默认的Tomcat. private void cr ...

  5. Spring MVC启动过程(1):ContextLoaderListener初始化

    此文来自https://my.oschina.net/pkpk1234/blog/61971 (写的特别好)故引来借鉴 Spring MVC启动过程 以Tomcat为例,想在Web容器中使用Spirn ...

  6. 转:spring的启动过程-spring和springMVC父子容器的原理

    要想很好理解这三个上下文的关系,需要先熟悉spring是怎样在web容器中启动起来的.spring的启动过程其实就是其IoC容器的启动过程,对于web程序,IoC容器启动过程即是建立上下文的过程. s ...

  7. Spring Boot启动过程(七):Connector初始化

    Connector实例的创建已经在Spring Boot启动过程(四):Spring Boot内嵌Tomcat启动中提到了: Connector是LifecycleMBeanBase的子类,先是设置L ...

  8. Spring Boot启动过程及回调接口汇总

    Spring Boot启动过程及回调接口汇总 链接: https://www.itcodemonkey.com/article/1431.html 来自:chanjarster (Daniel Qia ...

  9. spring的启动过程就是创建ioc容器的过程

    1. spring简介 spring的最基本的功能就是创建对象及管理这些对象之间的依赖关系,实现低耦合.高内聚.还提供像通用日志记录.性能统计.安全控制.异常处理等面向切面的能力,还能帮我们管理最头疼 ...

随机推荐

  1. Disconf实践指南:改造篇

    上一篇文章Disconf实践指南:使用篇介绍了如何在项目中应用disconf,虽然实现了分布式配置的实时刷新,但是我们希望能够去除所有的配置文件,把配置都交给disconf管理,本地只需要实现配置监听 ...

  2. maven项目将web2.5改为web3.1

    用maven构建的web项目默认的web.xml为2.3的版本,而我们需要更改为我们想要的版本(3.1). 在这里有两种方式更改web.xml的版本: 第一种: 将项目切换为navigator视图,然 ...

  3. C# 提取方法重构

    引用:https://msdn.microsoft.com/zh-CN/library/0s21cwxk.aspx “提取方法”是一项重构操作,提供了一种从现有成员中的代码段创建新方法的便捷方式. 使 ...

  4. libaco: 一个极速的轻量级 C 非对称协程库 🚀 (10 ns/ctxsw + 一千万协程并发仅耗内存 2.8GB + Github Trending)

    0 Name 简介 libaco - 一个极速的.轻量级.C语言非对称协程库. 这个项目的代号是Arkenstone 

  5. python开发最受欢迎的十款工具

    python开发最受欢迎的十款工具 dreamyla3个月前 今天小编给正在学习python开发的朋友介绍十款最受欢迎的开发工具,因为在学习python开发过程中少不了IDE或者代码编辑器,想要提高开 ...

  6. JDBC模板对象是多例的

  7. 微服务性能优化之thrift改造

    在我当前所做的web项目中,采用前后端分离模式前端通过Django 提供restful接口,后端采用微服务架构,微服务之间的调用采用jsonrpc,由于微服务之间的调用很频繁,导致前端得到的响应很慢, ...

  8. Android 定时重复启动弹出窗口。

    本来想着用handlerpostdelay就可以实现,没想到演示后关闭应用居然报错. 后来想到是没有了activity. ((Activity)context).isFinishing() 可以传入c ...

  9. Specular Mask Texture

    [Specular Mask Texture] Specular Texture是一个Mask纹理,通过Mask纹理的texel可以控制每个像素的纹理. 在属性中添加Specular Mask Tex ...

  10. VUE,使用物理引擎Box2D设计类愤怒小鸟的击球游戏--基本架构设置