当前位置:网站首页>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;
}
}
边栏推荐
- Article download address
- Exness: positive I win, negative you lose
- Wechat video Number launches "creator traffic package"
- Interview question MySQL transaction (TCL) isolation (four characteristics)
- 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)
- Servlet learning notes
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 9
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 6
- Global and Chinese market of cardiac monitoring 2022-2028: Research Report on technology, participants, trends, market size and share
- SAP ui5 date type sap ui. model. type. Analysis of the display format of date
猜你喜欢

Paper notes ACL 2020 improving event detection via open domain trigger knowledge

C language array

Jetson TX2配置Tensorflow、Pytorch等常用库
![[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 20](/img/d5/4bce239b522696b5312b1346336b5f.jpg)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 20

昨天的事情想说一下
![[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 5](/img/68/4f92ca7cbdb90a919711b86d401302.jpg)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 5

Leetcode: 408 sliding window median

MySQL advanced review

VIM, another program may be editing the same file If this is the solution of the case

17. Memory partition and paging
随机推荐
Introduction to the button control elevatedbutton of the fleet tutorial (the tutorial includes the source code)
SAP ui5 date type sap ui. model. type. Analysis of the display format of date
IIS error, unable to start debugging on the webserver
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 17
Global and Chinese market for naval vessel maintenance 2022-2028: Research Report on technology, participants, trends, market size and share
DC-5靶机
C语言:求100-999是7的倍数的回文数
Kivy教程之 08 倒计时App实现timer调用(教程含源码)
Langue C: trouver le nombre de palindromes dont 100 - 999 est un multiple de 7
PKCs 5: password based cryptography specification version 2.1 Chinese Translation
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 12
Anti clockwise rotation method of event arrangement -- PHP implementation
The solution of permission denied
Jetson TX2 configures common libraries such as tensorflow and pytoch
Global and Chinese market of cardiac monitoring 2022-2028: Research Report on technology, participants, trends, market size and share
. Does net 4 have a built-in JSON serializer / deserializer- Does . NET 4 have a built-in JSON serializer/deserializer?
《天天数学》连载57:二月二十六日
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 11
Interview question MySQL transaction (TCL) isolation (four characteristics)
C语言:求字符串的长度