当前位置:网站首页>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;
}
}
边栏推荐
- Play Sanzi chess easily
- Experiment 7. IPv6
- Jetson TX2 configures common libraries such as tensorflow and pytoch
- How to disable debug messages on sockjs stomp - how to disable debug messages on sockjs Stomp
- Global and Chinese markets of NOx analyzers 2022-2028: Research Report on technology, participants, trends, market size and share
- Map container
- Anti clockwise rotation method of event arrangement -- PHP implementation
- Flet教程之 按钮控件 ElevatedButton入门(教程含源码)
- Unity performance optimization reading notes - Introduction (1)
- Talk about "in C language"
猜你喜欢

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
![Cadence physical library lef file syntax learning [continuous update]](/img/d5/0671935b074e538a2147dbe51a5a70.jpg)
Cadence physical library lef file syntax learning [continuous update]

2022, 6G is heating up
![[the way of programmer training] - 2 Perfect number calculation](/img/fd/4bb8560f601daddaa8895f20215be4.jpg)
[the way of programmer training] - 2 Perfect number calculation
![[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 18](/img/1a/94ef8be5c06c2d1c52fc8ce7f03ea7.jpg)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 18

JD home programmers delete databases and run away. Talk about binlog, the killer of MySQL data backup

记一次 Showing Recent Errors Only Command /bin/sh failed with exit code 1 问题

Introduction to the button control elevatedbutton of the fleet tutorial (the tutorial includes the source code)

Hongke case study on storm impact in coastal areas of North Carolina using lidar

DC-5靶机
随机推荐
Hongke case study on storm impact in coastal areas of North Carolina using lidar
When synchronized encounters this thing, there is a big hole, pay attention!
Recommend a cool geospatial data visualization tool with low code
Global and Chinese markets for soluble suture 2022-2028: Research Report on technology, participants, trends, market size and share
Servlet learning notes
Flet教程之 02 ElevatedButton高级功能(教程含源码)(教程含源码)
Iterm tab switching order
Source code analysis of the implementation mechanism of multisets in guava class library
nn. Exploration and experiment of batchnorm2d principle
Openssl3.0 learning 20 provider KDF
How to realize the function of Sub Ledger of applet?
Kivy tutorial 08 countdown app implements timer call (tutorial includes source code)
BCD code Baidu Encyclopedia
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 20
【数据聚类】第四章第一节3:DBSCAN性能分析、优缺点和参数选择方法
. Does net 4 have a built-in JSON serializer / deserializer- Does . NET 4 have a built-in JSON serializer/deserializer?
Global and Chinese markets of NOx analyzers 2022-2028: Research Report on technology, participants, trends, market size and share
A few words explain redis cache penetration, breakdown, avalanche, and redis sentinel
C fonctions linguistiques
Awk getting started to proficient series - awk quick start