当前位置:网站首页>AbstractDispatcherServletInitializer 的实现类为什么可以在初始化Web容器的时候被调用
AbstractDispatcherServletInitializer 的实现类为什么可以在初始化Web容器的时候被调用
2022-07-04 12:33:00 【Nishkata】
引入
在学习 Spring MVC 引入 Java Web项目的时候,发现除了使用 web.xml 注册 DispatcherServlet 的方式,还可以使用配置类继承 AbstractDispatcherServletInitializer 的方式进行 DispatcherServlet 这个前端控制器的注册。于是好奇我们写的 DispatcherServletInitializer 配置类怎么能被 Web 容器识别并加载。
解析
DispatcherServletInitializer 配置类能够被加载的核心是 Java 的 SPI 服务发现机制。
SPI 服务发现机制
- 服务接口 的定义: 某一个框架定义了某种服务的 接口 ,我们在使用这个框架的时候可以使用不同的服务提供商提供的接口的实现。
- 注册 服务接口 的实现: 在 classpath:META-INF/services/目录下创建一个以 服务接口 全类名命名的文件,内容为实现类的全类名。
- 服务加载 定义了 服务接口 的框架在启动时会到 SPI 规范指定的 classpath:META-INF/services/目录下寻找并加载此框架所需服务。
Spring MVC 遵循 SPI 规范,作为Servlet容器的服务提供商向其注册服务
对应于SPI 服务发现机制的三点
- 服务接口 的定义 Servlet 规范定义了服务接口 javax-servlet-api-xxx.jar/javax.servlet.ServletContainerInitializer
- 注册 服务接口 的实现: Spring MVC 注册服务。在 spring-web-xxx.jar 包下 有 META-INF/services/javax.servlet.ServletContainerInitializer 文件,内容为 org.springframework.web.SpringServletContainerInitializer
- 服务加载 SpringServletContainerInitializer 中的 onStartup 方法会在 Servlet 容器初始化的时候通过 SPI 机制发现并调用,onStartup 方法里面又会 将所有 WebApplicationInitializer 的实现类的对象的 onStartup 方法全部调用一遍。文章开头所说的 AbstractDispatcherServletInitializer 也是 WebApplicationInitializer 接口的实现,所以其 onStartup 方法也会在 Servlet 容器初始化的时候被调用。
展望
关于 SpringServletContainerInitializer 如何加载 WebApplicationInitializer 还有待研究。
参考
Java编程技术之浅析SPI服务发现机制
附录
继承 AbstractDispatcherServletInitializer 方式引入 Spring MVC
public class DispatcherServletInitializer extends AbstractDispatcherServletInitializer {
@Override
protected WebApplicationContext createServletApplicationContext() {
AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
applicationContext.register(SpringMvcConfig.class);
return applicationContext;
}
@Override
protected String[] getServletMappings() {
// "/" 所有,但不包括.jsp/.html; "/*" 表示包括.jsp/.html 的所有
return new String[]{
"/"};
}
@Override
protected WebApplicationContext createRootApplicationContext() {
return null;
}
}
边栏推荐
- IPv6 experiment
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 20
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 5
- Introduction to random and threadlocalrandom analysis
- Fastlane one click package / release app - usage record and stepping on pit
- The solution of permission denied
- Fastlane 一键打包/发布APP - 使用记录及踩坑
- Source code analysis of the implementation mechanism of multisets in guava class library
- [Chongqing Guangdong education] National Open University spring 2019 2727 tax basis reference questions
- Entitas learning [iv] other common knowledge points
猜你喜欢
The frost peel off the purple dragon scale, and the xiariba people will talk about database SQL optimization and the principle of indexing (primary / secondary / clustered / non clustered)
C language array
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 8
Review of week 278 of leetcode II
【数据聚类】第四章第一节3:DBSCAN性能分析、优缺点和参数选择方法
0x15 string
17. Memory partition and paging
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 19
VIM, another program may be editing the same file If this is the solution of the case
Show recent errors only command /bin/sh failed with exit code 1
随机推荐
[ES6] template string: `string`, a new symbol in es2015
Fastlane 一键打包/发布APP - 使用记录及踩坑
Introduction to random and threadlocalrandom analysis
vim 出现 Another program may be editing the same file. If this is the case 的解决方法
Global and Chinese markets for environmental disinfection robots 2022-2028: Research Report on technology, participants, trends, market size and share
JD home programmers delete databases and run away. Talk about binlog, the killer of MySQL data backup
MySQL advanced review
Pat 1059 prime factors (25 points) prime table
DC-5靶机
Show recent errors only command /bin/sh failed with exit code 1
The detailed installation process of Ninja security penetration system (Ninjitsu OS V3). Both old and new VM versions can be installed through personal testing, with download sources
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 12
Leetcode day 17
C language array
MySQL advanced (Advanced) SQL statement
Xshell's ssh server rejected the password, failed to skip publickey authentication, and did not register with the server
Jetson TX2 configures common libraries such as tensorflow and pytoch
SAP ui5 date type sap ui. model. type. Analysis of the display format of date
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 24
[solve the error of this pointing in the applet] SetData of undefined