当前位置:网站首页>Why can the implementation class of abstractdispatcherservletinitializer be called when initializing the web container
Why can the implementation class of abstractdispatcherservletinitializer be called when initializing the web container
2022-07-04 12:45:00 【Nishkata】
introduce
I'm learning Spring MVC introduce Java Web When the project is , Found that in addition to using web.xml register DispatcherServlet The way , You can also use configuration class inheritance AbstractDispatcherServletInitializer By DispatcherServlet Registration of this front-end controller . So curious about what we wrote DispatcherServletInitializer How can configuration classes be Web The container identifies and loads .
analysis
DispatcherServletInitializer The core that configuration classes can be loaded is Java Of SPI Service discovery mechanism .
SPI Service discovery mechanism
- Service interface The definition of : A framework defines the Interface , When using this framework, we can use the implementation of interfaces provided by different service providers .
- register Service interface The implementation of the : stay classpath:META-INF/services/ Create a directory to Service interface File named by full class name , The content is the full class name of the implementation class .
- Service load Defined Service interface The framework of will arrive at SPI Specified in the specification classpath:META-INF/services/ Find and load the services required by this framework under the directory .
Spring MVC follow SPI standard , As Servlet The service provider of the container registers services with it
Corresponding to SPI Three points of service discovery mechanism
- Service interface The definition of Servlet The specification defines the service interface javax-servlet-api-xxx.jar/javax.servlet.ServletContainerInitializer
- register Service interface The implementation of the : Spring MVC Registration service . stay spring-web-xxx.jar It's a bag Yes META-INF/services/javax.servlet.ServletContainerInitializer file , The content is org.springframework.web.SpringServletContainerInitializer
- Service load SpringServletContainerInitializer Medium onStartup Methods in Servlet The container is initialized by SPI The mechanism discovers and invokes ,onStartup In the method Will all WebApplicationInitializer Of the object of the implementation class onStartup Methods are all called once . At the beginning of the article AbstractDispatcherServletInitializer It's also WebApplicationInitializer Interface implementation , So its onStartup The method will also be in Servlet Called when the container is initialized .
expectation
About SpringServletContainerInitializer How to load WebApplicationInitializer It needs to be studied .
Reference resources
Java Analysis of programming technology SPI Service discovery mechanism
appendix
Inherit AbstractDispatcherServletInitializer Mode introduction 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() {
// "/" all , But does not include .jsp/.html; "/*" Means to include .jsp/.html All of the
return new String[]{
"/"};
}
@Override
protected WebApplicationContext createRootApplicationContext() {
return null;
}
}
边栏推荐
- Sort merge sort
- 13、 C window form technology and basic controls (3)
- When synchronized encounters this thing, there is a big hole, pay attention!
- Cadence physical library lef file syntax learning [continuous update]
- The most robust financial products in 2022
- Wechat video Number launches "creator traffic package"
- Leetcode day 17
- When synchronized encounters this thing, there is a big hole, pay attention!
- Langue C: trouver le nombre de palindromes dont 100 - 999 est un multiple de 7
- asp. Core is compatible with both JWT authentication and cookies authentication
猜你喜欢
R language -- readr package reads and writes data
Communication tutorial | overview of the first, second and third generation can bus
Entitas learning [3] multi context system
ArcGis利用栅格处理工具进行影像裁剪
Argminer: a pytorch package for processing, enhancing, training, and reasoning argument mining datasets
C语言函数
Fastlane 一键打包/发布APP - 使用记录及踩坑
16.内存使用与分段
ArgMiner:一个用于对论点挖掘数据集进行处理、增强、训练和推理的 PyTorch 的包
2022, 6G is heating up
随机推荐
[solve the error of this pointing in the applet] SetData of undefined
[ES6] template string: `string`, a new symbol in es2015
Error: Failed to download metadata for repo ‘AppStream‘: Cannot download repomd. XML solution
Method of setting default items in C # ComboBox control code
French Data Protection Agency: using Google Analytics or violating gdpr
Bottom Logic -- Mind Map
When to use pointers in go?
Introduction to random and threadlocalrandom analysis
Full arrangement (medium difficulty)
03_ Armv8 instruction set introduction load and store instructions
R language -- readr package reads and writes data
16.内存使用与分段
When synchronized encounters this thing, there is a big hole, pay attention!
C fonctions linguistiques
Flet教程之 按钮控件 ElevatedButton入门(教程含源码)
mm_ Cognition of struct structure
priority_ queue
SAP ui5 date type sap ui. model. type. Analysis of the display format of date
C language: find the palindrome number whose 100-999 is a multiple of 7
Entitas learning [iv] other common knowledge points