I am trying to find out in spring-boot
, which implementation of WebApplicationInitializer
actually register the dispatcher servlet.
I didn't found any piece code from SpringBootServletInitializer
or its parent types did that.
Instead, AbstractDispatcherServletInitializer
does the job, but it's abstract, I can't find any of its concrete implementation with help of Eclipse.
So, which piece of code from which class is actually invoked to register the dispatcher servlet for springMVC?
This is a subsequent question of: How does spring-boot able to serve specific url?
Below is the description of Spring Boot initialization steps that eventually register DispatcherServlet
.
@EnableAutoConfiguration
public class TestSpring {
public static void main(String[] args) throws Exception {
SpringApplication.run(TestSpring.class, args);
}
}
Here are the steps:
SpringApplication.run()
creates EmbeddedWebApplicationContext
application context; refresh()
method; TestSpring
. It looks for import annotations. EnableAutoConfiguration
is one of them. For an import annotation the refresh process gets the corresponding class from the annotation value
and invokes its selectImports()
method; @EnableAutoConfiguration
the corresponding class is EnableAutoConfigurationImportSelector
whose selectImports()
loads tons of other import selectors from the META-INF/spring.factories
; @Bean
annotation, ie beans that require the Spring context to call the corresponding method automatically to instantiate them; resfresh()
continues and reaches onRefresh()
, the createEmbeddedServletContainer()
method is called inside; ServletContextInitializer
are searched for and instantiated. One of them is the bean, defined by the DispatcherServletAutoConfiguration.DispatcherServletRegistrationConfiguration#dispatcherServletRegistration()
method of ServletRegistrationBean
type that extends ServletContextInitializer
. As you can guess from the name of the class, such initializers add a given servlet (in this case DispatcherServlet ) to a given ServletContext
, when their onStartup()
method is invoked; ServletContextInitializer
s at the previous step are passed to this tomcat initialization - this is where the onStartup()
methods of those ServletContextInitializer
s are called and DispatcherServlet
gets created and registered as servlet ; onRefresh()
of application context; finishRefresh()
is called where tomcat is finally started by TomcatEmbeddedServletContainer.start()
; refresh()
of application context and other final initialization steps; When you look for something Spring Boot does during auto-configuration, you should look in the *AutoConfiguration
classes. In this case, DispatcherServletAutoConfiguration
.
If you want to deploy the Spring Boot application as a traditional WAR, then the Servlet 3.0 specification details how service providers can set it up so that the 3.0 compliant servlet container will automatically bootstrap any web assets (Servlet, Filter, ServletContextInitializer's) into the servlet container. The "magic" is accomplished in the spring-web dependency. If you un'jar it, you'll find file "spring-web/META-INF/services/javax.servlet.ServletContainerInitializer". Open the file and you'll see single line "org.springframework.web.SpringServletContainerInitializer". This class delegates to all classes that implement WebApplicationInitializer , more specifically their onStartup(ServletContext servletContext) method. There's one such concrete class I'm aware of in Spring Boot, namely SpringBootServletInitializer .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.