当前位置:网站首页>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

  1. 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 .
  2. 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 .
  3. 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

  1. Service interface The definition of Servlet The specification defines the service interface javax-servlet-api-xxx.jar/javax.servlet.ServletContainerInitializer
  2. 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
  3. 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;
    }
}
原网站

版权声明
本文为[Nishkata]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207041001177233.html

随机推荐