细说Spring Boot初始化DispatcherServlet
DispatcherServlet概述
在Spring Boot框架未出现之前,要开发一个基于Spring MVC框架的项目,通常需要在Java web项目的描述符文件web.xml中添加如下配置:
<!-- 初始化Spring IoC容器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 手动配置DispatcherServlet -->
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/spring*.xml</param-value>
</init-param>
</servlet>
<!-- 配置DispatcherServlet拦截路径,让所有Web请求都经过DispatcherServlet -->
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
添加如上配置之后,就能实现将Spring MVC框架整合到Web项目中了,那这是怎么做到的呢?下面一一解答下。
首先,org.springframework.web.context.ContextLoaderListener实现了接口javax.servlet.ServletContextListener,这样就能确保Web应用在启动后回调该接口。
public interface ServletContextListener extends EventListener {
// Web应用在启动之后会调用该方法,通过传递的事件参数获取到ServletContext上下文环境
public void contextInitialized(ServletContextEvent sce);
// Web应用在停止后会回调该方法
public void contextDestroyed(ServletContextEvent sce);
}
其次,在org.springframework.web.context.ContextLoaderListener中实现了contextInitialized()方法,并且在方法实现中触发了对Spring IoC容器的初始化,并且将ServletContext上下文保存到了IoC容器中。
// ServletContextListener
@Override
public void contextInitialized(ServletContextEvent event) {
// 触发对Spring IoC容器的初始化
initWebApplicationContext(event.getServletContext());
}
// AbstractRefreshableWebApplicationContext
@Override
public void setServletContext(@Nullable ServletContext servletContext) {
// 在IoC容器中保存了ServletContext上下文
this.servletContext = servletContext;
}
既然Spring IoC容器已经和ServletContext建立了联系,那当Web请求被DispatcherServlet拦截之后就可以基于Spring IoC容器环境进行处理了,实际上这样就建立了一个从Serlvet到Spring MVC框架的桥梁。
至此,关于Spring MVC框架如何与Web应用集成的问题就算得到了解答,很显然在传统的Spring MVC项目中,这是通过手动配置实现的。而在使用Spring Boot框架时就没有再看到这些配置了, Spring Boot的强大之处在于自动装配机制,虽然我们没有手动去配置,实际上是Spring Boot框架帮我们自动实现了。
那么,Spring Boot是如何实现的呢?
Spring Boot自动初始化DispatcherServlet
如下解读基于Spring Boot 2.7.14版本进行。
经过对Spring Boot的源码解读和梳理后知道,在Spring Boot框架中DispatcherServlet的自动装配是通过注解@DispatcherServletAutoConfiguration实现的。
具体流程如下:
首先,在Spring Boot的核心注解@EnableAutoConfiguration中引入了一个类AutoConfigurationImportSelector,Spring Boot在启动时会触发该类中如下方法的调用。
// AutoConfigurationImportSelector.getCandidateConfigurations()
// 该方法是在Spring Boot启动时调用的,具体来说是在刷新Spring IoC容器的时候触发的
// 在该方法中实现加载自动配置类,具体来说,是加载2个配置文件中的自动配置类
// 其一,加载各种starter组件jar中"META-INF/spring.factories"文件指定的自动配置类
// 其二,加载Sring Boot自己的自动配置类,这些类在文件"META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports"中指定
protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
// 首先加载各类starter组件jar包中"META-INF/spring.factories"文件指定的自动配置类
List<String> configurations = new ArrayList<>(
SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(), getBeanClassLoader()));
// 再加载文件"META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports"中指定的自动配置类
ImportCandidates.load(AutoConfiguration.class, getBeanClassLoader()).forEach(configurations::add);
return configurations;
}
其次,在加载Spring Boot的“META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports”自动配置类的时候,就会加载到org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration。

在DispatcherServletAutoConfiguration的源码中可以很清晰地看到正是在该配置类中实现了对dispatcherServlet的注入。
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
@AutoConfiguration(after = ServletWebServerFactoryAutoConfiguration.class)
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass(DispatcherServlet.class)
public class DispatcherServletAutoConfiguration {
@Configuration(proxyBeanMethods = false)
@Conditional(DefaultDispatcherServletCondition.class)
@ConditionalOnClass(ServletRegistration.class)
@EnableConfigurationProperties(WebMvcProperties.class)
protected static class DispatcherServletConfiguration {
// 实际上是在DispatcherServletAutoConfiguration的内部静态类DispatcherServletConfiguration中完成了对"dispatcherServlet"的注入
@Bean(name = DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)
public DispatcherServlet dispatcherServlet(WebMvcProperties webMvcProperties) {
DispatcherServlet dispatcherServlet = new DispatcherServlet();
dispatcherServlet.setDispatchOptionsRequest(webMvcProperties.isDispatchOptionsRequest());
dispatcherServlet.setDispatchTraceRequest(webMvcProperties.isDispatchTraceRequest());
dispatcherServlet.setThrowExceptionIfNoHandlerFound(webMvcProperties.isThrowExceptionIfNoHandlerFound());
dispatcherServlet.setPublishEvents(webMvcProperties.isPublishRequestHandledEvents());
dispatcherServlet.setEnableLoggingRequestDetails(webMvcProperties.isLogRequestDetails());
return dispatcherServlet;
}
// 省略了其他代码
// ...
}
}
虽然解答了DispatcherServlet的自动注入问题,但是还没有解答Spring IoC容器是如何与ServletContext上下文建立联系的。
经过对Spring Boot启动流程的源码解读知道,其实建立Spring IoC容器与ServletContext的关系是在ServletWebServerApplicationContext.onRefresh()方法中实现的。
// 在刷新Spring IoC容器的过程中会调用该方法
@Override
protected void onRefresh() {
super.onRefresh();
try {
createWebServer();
}
catch (Throwable ex) {
throw new ApplicationContextException("Unable to start web server", ex);
}
}
private void createWebServer() {
// 在这里会完成对Servlet容器的创建,并且与ServletContext上下文进行关联
}
所以,Spring Boot通过自动装配机制完成了对Spring MVC的DispatcherServlet注入,并且还创建了嵌入式的Servlet容器,并以Deamon线程方式运行在后台。
细说Spring Boot初始化DispatcherServlet的更多相关文章
- 1.spring boot初始化项目
初始化spring boot项目的方式非常多,如使用Spring Tool Suite.使用IntelliJ IDEA.使用NetBeans.在start.spring.io网站中.curl命令.sp ...
- Spring Boot 初始化运行特定方法
Spring Boot提供了两种 “开机自启动” 的方式,ApplicationRunner和CommandLineRunner 这两种方式的目的是为了满足,在容器启动时like执行某些方法.我们可以 ...
- Spring Boot整合Dubbo使用及开发笔记
一.概述: Spring Dubbo是我开发的一个基于spring-boot和dubbo,目的是使用Spring boot的风格来使用dubbo.(即可以了解Spring boot的启动过程又可以学习 ...
- Spring Boot 中的静态资源到底要放在哪里?
当我们使用 SpringMVC 框架时,静态资源会被拦截,需要添加额外配置,之前老有小伙伴在微信上问松哥Spring Boot 中的静态资源加载问题:"松哥,我的HTML页面好像没有样式?& ...
- Spring Boot 2.0 配置图文教程
摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠BYSocket 」欢迎关注和转载,保留摘要,谢谢! 本章内容 自定义属性快速入门 外化配置 自动配置 自定义创建 ...
- spring boot到底帮我们做了那些事?
一.前言 上一篇介绍了注解,也是为这一篇做铺垫,传统的都是通过配置文件来启动spring,那spring boot到底是做了什么能让我们快速开发昵? 二.启动原理 看下程序启动的入口, ...
- Spring boot+ logback环境下,日志存放路径未定义的问题
日志路径未定义 环境:Spring boot + logback 配置文件: <configuration> <springProfile name="dev"& ...
- idea中使用tomcat 方式启动spring boot项目
Spring boot 的main 入口启动方式相信都会用,直接运行main直接就启动了,但是往往这种方式并不是最佳的启动方式,比如运维的层面更希望调整tomcat的调优参数,而只使用嵌入启动方式很难 ...
- Spring Boot 构建一个 RESTful Web Service
1 项目目标: 构建一个 web service,接收get 请求 http://localhost:8080/greeting 响应一个json 结果: {"id":1,&qu ...
- 创建Spring Boot项目的几种方式总结
一.我们可以使用Spring Initializr来创建SpringBoot项目. Spring Initializr从本质上来说就是一个Web应用程序,它能为你生成Spring Boot项目结构.虽 ...
随机推荐
- [转帖]--build=arm-linux
今天在arm上用configure生成makefile时报错:configure: error: cannot guess build type; you must specify one 问题: 不 ...
- [转帖]JMeter InfluxDB v2.0 listener plugin
https://github.com/mderevyankoaqa/jmeter-influxdb2-listener-plugin Support my Ukrainian Family ️ Lik ...
- 查找linux下面某目录下重名出现的文件以及次数
find . -name '*.data' -exec basename {} \;| sort | uniq -w32 --all-repeated=separate | uniq -c | sor ...
- 麒麟信安V3.4 安装PG15的过程
麒麟信安V3.4 安装PG15的过程 背景 发现基于OpenEuler的几个系统使用CentOS的rpm包 安装PG数据库时有问题. 会提示缺少依赖的so文件. 今天想着解决一下, 就百度了一下并且进 ...
- SPI在Java中的实现与应用 | 京东物流技术团队
1 SPI的概念 API API在我们日常开发工作中是比较直观可以看到的,比如在 Spring 项目中,我们通常习惯在写 service 层代码前,添加一个接口层,对于 service 的调用一般也都 ...
- Redis极简教程
简介 Redis 是用C语言开发完全开源免费的,遵守BSD协议的,一个高性能的,key-value型的,NOSQL数据库. 特点 可以将内存中的数据持久化到硬盘中,重启的时候可以从硬盘中再次加载 拥有 ...
- 任意文件下载包含https的图片
使用a标签进行下载 <a href="https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ. ...
- 震惊p div 标签 可以编辑高度随内容的编辑而发生变化
震惊p标签可以编辑高度随内容的编辑而发生变化### 1==>只可编辑,粘贴复制字段长度不正常 <p contenteditable="true" >这是一个可编辑 ...
- 你不知道的Promise状态变化机制
1.Promise中PromiseStatus的三种状态 var p = new Promise((resolve, reject) => { // resolve 既是函数也是参数,它用于处理 ...
- gpedit.msc 打不开
win10系统推出已有不短的时间了,朋友们也纷纷升级了win10系统,但是暴露的问题也是越来越多,比如win10系统打开运行输入gpedit.msc命令时却提示找不到文件.那出现win10打不开gpe ...