外置tomcat 启动Spring 源码分析详解

tomcat 启动流程见: tomcat 源码分析_caicongyang的博客-CSDN博客

1. 核心类

org.springframework.web.SpringServletContainerInitializer 是通过spring-web 包中SPI 加载javax.servlet.ServletContainerInitializer的实现;

本例的 Application通过继承SpringBootServletInitializer来提供tomcat 的调用onStartUp方法

同时也写了一个 SpringApplication.run(Application.class); 来支持 本地main 方法启动;

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. SpringBootServletInitializer 中的onStartUp 方法

  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");
        }

    }

猜你喜欢

转载自blog.csdn.net/caicongyang/article/details/125826841
今日推荐