Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Tomcat exactly bootstrap the app without web.xml?

Tags:

java

spring

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

like image 604
Finkelson Avatar asked Sep 13 '15 13:09

Finkelson


People also ask

Do we need web xml in Spring boot application?

Not even a web. xml file is required! When required, however, we can take control over parts of the configuration and override the conventions that Spring Boot puts in play.

Is Web xml necessary?

Since the servlet 3 specification (IIRC) a web. xml is not needed anymore.


1 Answers

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.

like image 180
Paul Samsotha Avatar answered Nov 15 '22 16:11

Paul Samsotha