Web环境中Spring的启动过程
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的工作流程分析完毕。
Web环境中Spring的启动过程的更多相关文章
- web环境中的spring MVC
1. web.xml文件的简单详解 在web环境中, spring MVC是建立在IOC容器的基础上,要了解spring mvc,首先要了解Spring IOC容器是如何在web环境中被载入并起作用的 ...
- ssm框架中,项目启动过程以及web.xml配置详解
原文:https://blog.csdn.net/qq_35571554/article/details/82385838 本篇主要在基于SSM的框架,深入讲解web.xml的配置 web.xml ...
- Spring IOC容器在Web容器中是怎样启动的
前言 我们一般都知道怎样使用spring来开发web应用后,但对spring的内部实现机制通常不是很明白.这里从源码角度分析下Spring是怎样启动的.在讲spring启动之前,我们先来看看一个web ...
- Spring Boot启动过程(四):Spring Boot内嵌Tomcat启动
之前在Spring Boot启动过程(二)提到过createEmbeddedServletContainer创建了内嵌的Servlet容器,我用的是默认的Tomcat. private void cr ...
- Spring MVC启动过程(1):ContextLoaderListener初始化
此文来自https://my.oschina.net/pkpk1234/blog/61971 (写的特别好)故引来借鉴 Spring MVC启动过程 以Tomcat为例,想在Web容器中使用Spirn ...
- 转:spring的启动过程-spring和springMVC父子容器的原理
要想很好理解这三个上下文的关系,需要先熟悉spring是怎样在web容器中启动起来的.spring的启动过程其实就是其IoC容器的启动过程,对于web程序,IoC容器启动过程即是建立上下文的过程. s ...
- Spring Boot启动过程(七):Connector初始化
Connector实例的创建已经在Spring Boot启动过程(四):Spring Boot内嵌Tomcat启动中提到了: Connector是LifecycleMBeanBase的子类,先是设置L ...
- Spring Boot启动过程及回调接口汇总
Spring Boot启动过程及回调接口汇总 链接: https://www.itcodemonkey.com/article/1431.html 来自:chanjarster (Daniel Qia ...
- spring的启动过程就是创建ioc容器的过程
1. spring简介 spring的最基本的功能就是创建对象及管理这些对象之间的依赖关系,实现低耦合.高内聚.还提供像通用日志记录.性能统计.安全控制.异常处理等面向切面的能力,还能帮我们管理最头疼 ...
随机推荐
- Git学习之常用的命令
配置git git config --global user.name "你的github用户名" git config --global user.email "你的G ...
- 给Array本地对象增加一个原型方法,它用于删除数组条目中重复的条目(可能有多个),返回值是一个包含被删除的重复条目的新数组
Array.prototype.removeCount=function(){ var that=this; var arr=[]; for(var i=0;i<that.length;i++) ...
- Vue.js+Koa2移动电商 笔记
一.搭建项目架构: 项目采用Webpack+Vue-router的架构方式,开始安装(基于windows系统) 1.按Win+R,然后在文本框中输入cmd,回车打开命令行,输入vue-cli安装命令: ...
- leetcode125
public class Solution { Stack<char> S = new Stack<char>(); Queue<char> Q = new Que ...
- Android的SearchView详解
转自https://www.jianshu.com/p/7c1e78e91506 一.简述 SearchView是v7包中的一个兼容性控件,它可以单独使用,也可以配合menu+toolbar一起使用. ...
- 『PLSQL』在oracle表中怎样创建自增长字段?
1.建立测试数据表CREATE TABLE TEST( ID NUMBER, NAME VARCHAR2(20), PRIMARY KEY(ID)); 2.创建序列CREATE SEQUENCE SE ...
- win10 wsl安装 命令行
用于一些精简版没有商店的安装方法 开启"Windows Subsystem for Linux" 可选特性 打开`PowerShell`,运行下面指令: Enable-Window ...
- go_指针
值传递:相当于在内存中拷贝一分变量出来,拷贝变量的改变对原变量不影响 引用传递:直接引用内存中的变量,会被改变 c,c++值传递和引用传递都有 Java,python多用引用传递 go语言指针简单之处 ...
- 【LA3126 训练指南】出租车 【DAG最小路径覆盖】
题意 你在一座城市里负责一个大型活动的接待工作.明天将有m位客人从城市的不同的位置出发,到达他们各自的目的地.已知每个人的出发时间,出发地点和目的地.你的任务是用尽量少的出租车送他们,使得每次出租车接 ...
- Node.js中流程控制
Node.js中的流程控制可以使用async,在使用之前需要先安装,使用npm安装 npm install async --g 下面主要介绍4种流程控制的方式: 1.串行无关联:async.serie ...