当前位置:网站首页>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;
}
}
边栏推荐
- [notes] streamingassets
- C语言:围圈报号排序问题
- 'using an alias column in the where clause in PostgreSQL' - using an alias column in the where clause in PostgreSQL
- Article download address
- ASP. Net razor – introduction to VB loops and arrays
- C language: find the length of string
- Iframe to only show a certain part of the page
- R language -- readr package reads and writes data
- 17. Memory partition and paging
- Interview question MySQL transaction (TCL) isolation (four characteristics)
猜你喜欢

Single spa, Qiankun, Friday access practice

17. Memory partition and paging

Servlet learning notes

Full arrangement (medium difficulty)

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

Experiment 7. IPv6

2022, 6G is heating up
![[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 24](/img/2e/b1f348ee6abaef24b439944acf36d8.jpg)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 24

昨天的事情想说一下

A treasure open source software, cross platform terminal artifact tabby
随机推荐
DVWA range exercise 4
How to use "bottom logic" to see the cards in the world?
Global and Chinese markets of NOx analyzers 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese market of dental elevators 2022-2028: Research Report on technology, participants, trends, market size and share
分布式事务相关概念与理论
Recommend a cool geospatial data visualization tool with low code
[the way of programmer training] - 2 Perfect number calculation
C語言函數
Memory computing integration: AI chip architecture in the post Moorish Era
When synchronized encounters this thing, there is a big hole, pay attention!
VBA, JSON interpretation, table structure -json string conversion
Talk about "in C language"
When synchronized encounters this thing, there is a big hole, pay attention!
Entity framework calls Max on null on records - Entity Framework calling Max on null on records
C language: find the length of string
Servlet learning notes
22 API design practices
Understand bloomfilter in one article
C语言:求字符串的长度
Flet教程之 按钮控件 ElevatedButton入门(教程含源码)