I'm wondering how does Tomcat
bootstrap my app on Spring MVC?
I have an initializer:
public class AppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) {
AnnotationConfigWebApplicationContext rootCtx = new AnnotationConfigWebApplicationContext();
rootCtx.register(AppConfig.class);
container.addListener(new ContextLoaderListener(rootCtx));
AnnotationConfigWebApplicationContext dispatcherCtx = new AnnotationConfigWebApplicationContext();
dispatcherCtx.register(FreeMarkerWebConfig.class);
ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(dispatcherCtx));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
I know why we need web.xml
and how Tomcat
uses it to bootstrap the app. But I don't understand how does Tomcat
know which servlet it should use to bootstrap the application if there are no xml
files, but only AppAppInitializer
?
Dependencies
<!-- spring mvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
<!-- servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>
...
I found this class in Spring core SpringServletContainerInitializer
. Is it correct that Tomcat
uses it to bootstrap my app?
http://docs.oracle.com/javaee/7/api/javax/servlet/ServletContainerInitializer.html?is-external=true