10

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

Finkelson
  • 2,921
  • 4
  • 31
  • 49

2 Answers2

22

Servlet 3.0 added a pluggability mechanism. How it works is that when your app is loaded, the Servlet container scans the classpath for a file named javax.servlet.ServletContainerInitializer inside META-INF/services. The contents of the file should simply be names of implementations of the initializer that the Servlet container can load. You can see this file in the spring-web jar. It lists org.springframework.web.SpringServletContainerInitializer as the implementation of the initializer.

How the Spring initializer works, is that it is passed all implementations (on the classpath) of WebApplicationInializer by the Servlet container. So how does the Servlet container know to pass these implementations? If you look at the source code for the inializer, you will see

@HandlesTypes(WebApplicationInitializer.class)
public class SpringServletContainerInitializer implements ServletContainerInitializer {

It is the @HandlesType annotation. All classes and even annotations1 listed in the @HandlesTypes will get picked up by the servlet container and passed to the SevletContainerInitializer through the single callback method argument

void onStartup(java.util.Set<java.lang.Class<?>> c, ServletContext ctx)

The Set argument contains all the implementations picked up by the Servlet container while scanning. You can look through the source code to see what Spring does with those implementations. It basically just calls the onStartup of all the inializers, passing in the ServletContext.


1. That sounded a bit unclear (and explaining it above would have been going a bit off on a tangent) so I'll just post it as an extra here. Imagine the @HandlesType instead was

@HandlesTypes({WebApplicationInitializer.class, Controller.class})
public class SpringServletContainerInitializer implements ServletContainerInitializer {

This means that the servlet container will also scan for classes annotated with @Controller, and also pass those onto the onStartup of the Spring initializer.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
2

It's connected with a fact, that Servlet 3.0 API is working without web.xml. So, it's not a Spring feature.

More info here: https://blogs.oracle.com/swchan/entry/servlet_3_0_annotations

JSR 315: JavaTM Servlet 3.0 Specification: https://jcp.org/en/jsr/detail?id=315

UPD:

Spring WebApplicationInitializer provides a programatic way to configure the Spring DispatcherServlet and ContextLoaderListener in Servlet 3.0+ compliant servlet containers , rather than adding this configuration through a web.xml file.

The answer is the ServletContainerInitializer interface introduced with Servlet 3.0 specification, implementors of this interface are notified during the context startup phase and can perform any programatic registration through the provided ServletContext.

Spring implements the ServletContainerInitializer through SpringServletContainerInitializer class. Per the Servlet specs, this implementation must be declared in a META-INF/services/javax.servlet.ServletContainerInitializer file of the libraries jar file - Spring declares this in spring-web*.jar jar file and has an entry org.springframework.web.SpringServletContainerInitializer

SpringServletContainerInitializer class has a @HandlerTypes annotation with a value of WebApplicationInitializer, this means that the Servlet container will scan for classes implementing the WebApplicationInitializer implementation and call the onStartUp method with these classes and that is where the WebApplicationInitializer fits in.

A little convoluted, but the good thing is all these details are totally abstracted away within the spring-web framework and the developer only has to configure an implementation of WebApplicationInitializer and live in a web.xml free world.

Oleksandr Loushkin
  • 1,479
  • 12
  • 21