当前位置:网站首页>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
- Pat 1059 prime factors (25 points) prime table
- Mongodb vs mysql, which is more efficient
- Full arrangement (medium difficulty)
- 轻松玩转三子棋
- . Does net 4 have a built-in JSON serializer / deserializer- Does . NET 4 have a built-in JSON serializer/deserializer?
- Recommend a cool geospatial data visualization tool with low code
- What if the chat record is gone? How to restore wechat chat records on Apple Mobile
- SAP ui5 date type sap ui. model. type. Analysis of the display format of date
- R language -- readr package reads and writes data
猜你喜欢
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)
分布式事务相关概念与理论
Daily Mathematics Series 57: February 26
Jetson TX2 configures common libraries such as tensorflow and pytoch
昨天的事情想说一下
SAP ui5 date type sap ui. model. type. Analysis of the display format of date
ArcGis利用栅格处理工具进行影像裁剪
LVS load balancing cluster deployment - Dr direct routing mode
16.内存使用与分段
Communication tutorial | overview of the first, second and third generation can bus
随机推荐
Cadence physical library lef file syntax learning [continuous update]
Daily Mathematics Series 57: February 26
DC-5 target
Global and Chinese market of piston rod 2022-2028: Research Report on technology, participants, trends, market size and share
The latest idea activation cracking tutorial, idea permanent activation code, the strongest in history
Unity performance optimization reading notes - Introduction (1)
Wechat video Number launches "creator traffic package"
Uva536 binary tree reconstruction tree recovery
Openssl3.0 learning 20 provider KDF
[ES6] template string: `string`, a new symbol in es2015
Global and Chinese markets for environmental disinfection robots 2022-2028: Research Report on technology, participants, trends, market size and share
'using an alias column in the where clause in PostgreSQL' - using an alias column in the where clause in PostgreSQL
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 23
[data clustering] section 3 of Chapter 4: DBSCAN performance analysis, advantages and disadvantages, and parameter selection methods
Googgle guava ImmutableCollections
C language: find the length of string
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
Anti clockwise rotation method of event arrangement -- PHP implementation
Kivy tutorial 08 countdown app implements timer call (tutorial includes source code)
Entity framework calls Max on null on records - Entity Framework calling Max on null on records