Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
163 views
in Technique[技术] by (71.8m points)

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...