当前位置:网站首页>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;
}
}
边栏推荐
- 01. Basics - MySQL overview
- Langue C: trouver le nombre de palindromes dont 100 - 999 est un multiple de 7
- How to use "bottom logic" to see the cards in the world?
- Hongke case study on storm impact in coastal areas of North Carolina using lidar
- Global and Chinese markets of NOx analyzers 2022-2028: Research Report on technology, participants, trends, market size and share
- How to use the mongodb ID array to get multiple documents- How to get multiple document using array of MongoDb id?
- [Android reverse] function interception instance (③ refresh CPU cache | ④ process interception function | ⑤ return specific results)
- Global and Chinese market of dental elevators 2022-2028: Research Report on technology, participants, trends, market size and share
- Global and Chinese markets of digital PCR and real-time PCR 2022-2028: Research Report on technology, participants, trends, market size and share
- asp. Core is compatible with both JWT authentication and cookies authentication
猜你喜欢

IPv6 experiment

Bottom Logic -- Mind Map

Communication tutorial | overview of the first, second and third generation can bus

2022, 6G is heating up

Daily Mathematics Series 57: February 26

ASP. Net razor – introduction to VB loops and arrays
![[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 19](/img/7c/f728e88ca36524f92c56213370399b.jpg)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 19

DVC use case (VI): Data Registry
![[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 9](/img/ed/0edff23fbd3880bc6c9dabd31755ac.jpg)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 9

C language function
随机推荐
Kivy tutorial 08 countdown app implements timer call (tutorial includes source code)
In 2022, financial products are not guaranteed?
Wechat video Number launches "creator traffic package"
Googgle guava ImmutableCollections
C language: find the length of string
. Does net 4 have a built-in JSON serializer / deserializer- Does . NET 4 have a built-in JSON serializer/deserializer?
Translation D29 (with AC code POJ 27:mode of sequence)
Lecture 9
[Yu Yue education] 233 pre school children's language education reference questions in the spring of 2019 of the National Open University
DC-5靶机
C语言函数
'using an alias column in the where clause in PostgreSQL' - using an alias column in the where clause in PostgreSQL
《天天数学》连载57:二月二十六日
How to realize the function of Sub Ledger of applet?
[solve the error of this pointing in the applet] SetData of undefined
Here, the DDS tutorial you want | first experience of fastdds - source code compilation & Installation & Testing
Openssl3.0 learning 20 provider KDF
Lvs+kept highly available cluster
C language: find the palindrome number whose 100-999 is a multiple of 7
Cadence physical library lef file syntax learning [continuous update]