Detailed explanation of external tomcat startup Spring source code analysis

For the tomcat startup process, see:  tomcat source code analysis_caicongyang's blog-CSDN blog

1. Core classes

org.springframework.web.SpringServletContainerInitializer is the implementation of javax.servlet.ServletContainerInitializer loaded through SPI in the spring-web package;

 

The Application in this example provides tomcat's onStartUp method by inheriting SpringBootServletInitializer.

At the same time, a SpringApplication.run(Application.class); was also written to support local main method startup;

public class Application extends SpringBootServletInitializer {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }
}

2. onStartUp method in SpringBootServletInitializer

  public void onStartup(ServletContext servletContext) throws ServletException {
        this.logger = LogFactory.getLog(this.getClass());
    //创建spring 上下文
        WebApplicationContext rootAppContext = this.createRootApplicationContext(servletContext);
        if (rootAppContext != null) {
            servletContext.addListener(new ContextLoaderListener(rootAppContext) {
                public void contextInitialized(ServletContextEvent event) {
                }
            });
        } else {
            this.logger.debug("No ContextLoaderListener registered, as createRootApplicationContext() did not return an application context");
        }

    }

Guess you like

Origin blog.csdn.net/caicongyang/article/details/125826841