zoukankan      html  css  js  c++  java
  • spring boot 在什么时候启动的tomcat

    我一直很好奇 spring boot 以哪种方式 启动的 tomcat  今天 特地跟踪了一下

    大家都知道 spring 容器很核心的 方式 是org.springframework.context.support.AbstractApplicationContext#refresh 这个方法

    其中 

                try {
                    // Allows post-processing of the bean factory in context subclasses.
                    postProcessBeanFactory(beanFactory);
    
                    // Invoke factory processors registered as beans in the context.
                    invokeBeanFactoryPostProcessors(beanFactory);
    
                    // Register bean processors that intercept bean creation.
                    registerBeanPostProcessors(beanFactory);
    
                    // Initialize message source for this context.
                    initMessageSource();
    
                    // Initialize event multicaster for this context.
                    initApplicationEventMulticaster();
    
                    // Initialize other special beans in specific context subclasses.
                    onRefresh();
    
                    // Check for listener beans and register them.
                    registerListeners();
    
                    // Instantiate all remaining (non-lazy-init) singletons.
                    finishBeanFactoryInitialization(beanFactory);
    
                    // Last step: publish corresponding event.
                    finishRefresh();
                }

    调用的是 org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext#onRefresh

    ps:在 springApplication.run 方法中 已jar包运行 启动的为org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext 这个上线文容器

    @Override
        protected void onRefresh() {
            super.onRefresh();
            try {
                createWebServer();
            }
            catch (Throwable ex) {
                throw new ApplicationContextException("Unable to start web server", ex);
            }
        }

    然后 是 org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory#getWebServer 

    @Override
        public WebServer getWebServer(ServletContextInitializer... initializers) {
            Tomcat tomcat = new Tomcat();
            File baseDir = (this.baseDirectory != null ? this.baseDirectory
                    : createTempDir("tomcat"));
            tomcat.setBaseDir(baseDir.getAbsolutePath());
            Connector connector = new Connector(this.protocol);
            tomcat.getService().addConnector(connector);
            customizeConnector(connector);
            tomcat.setConnector(connector);
            tomcat.getHost().setAutoDeploy(false);
            configureEngine(tomcat.getEngine());
            for (Connector additionalConnector : this.additionalTomcatConnectors) {
                tomcat.getService().addConnector(additionalConnector);
            }
            prepareContext(tomcat.getHost(), initializers);
            return getTomcatWebServer(tomcat);
        }
  • 相关阅读:
    mysql(一) 关联查询的方式
    SpringBoot2.0(五) CORS跨域
    SpringBoot2.0(四) 远程调试
    SpringBoot2.0(三) 文件上传
    SpringBoot2.0(二) 配置文件多环境
    SpringBoot2.0(一) mybatis
    Java InputStream转File
    git 命令学习
    reids 中出现 (error) MOVED 原因和解决方案
    ibm 的 heapanalyzer 分析器
  • 原文地址:https://www.cnblogs.com/rufus-hua/p/8504289.html
Copyright © 2011-2022 走看看