zoukankan      html  css  js  c++  java
  • springboot 的启动流程

    1.我们springboot 项目的启动类如下。

    方式1

    @SpringBootApplication
    public class SpringbootZkLockApplication {
    public static void main(String[] args) {
    SpringApplication.run(SpringbootZkLockApplication.class, args);
    }
    }
    点击 run 方法源码进入如下,
    /**
    * Static helper that can be used to run a {@link SpringApplication} from the
    * specified source using default settings.
    * @param primarySource the primary source to load
    * @param args the application arguments (usually passed from a Java main method)
    * @return the running {@link ApplicationContext}
    */
    public static ConfigurableApplicationContext run(Class<?> primarySource, String... args) {
    return run(new Class<?>[] { primarySource }, args);
    }
    继续 点击 run 方法进入源码,
    /**
    * Static helper that can be used to run a {@link SpringApplication} from the
    * specified sources using default settings and user supplied arguments.
    * @param primarySources the primary sources to load
    * @param args the application arguments (usually passed from a Java main method)
    * @return the running {@link ApplicationContext}
    */
    public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
    return new SpringApplication(primarySources).run(args);
    }
    我们看到 run() 方法的返回值为 ConfigurableApplicationContext。
    看到这里我们就知道了SpringBoot 启动类的底层 是 new SpringApplication(primarySources).run(args);
    这时候我们的启动类可以可以换成 如下方式启动。
    方式2
    @SpringBootApplication
    public class SpringbootZkLockApplication {

    public static void main(String[] args) {
    //SpringApplication.run(SpringbootZkLockApplication.class, args);
    new SpringApplication(SpringbootZkLockApplication.class).run(args);
    }
    new SpringApplication(SpringbootZkLockApplication.class).run(args); 可以拆分为2个部分启动
    方式3
    @SpringBootApplication
    public class SpringbootZkLockApplication {

    public static void main(String[] args) {
    //SpringApplication.run(SpringbootZkLockApplication.class, args);
    SpringApplication application = new SpringApplication(SpringbootZkLockApplication.class);
    application.run(args);
    }
    }
    上面这三种启动方式都是等价的,都可以启动 SpringBoot 项目。只不过是第一种方式是我们常用的,经过SpringBoot 封装过的启动方式。 
    @EnableAutoConfiguration 注解 加载了我们第三方配置的信息进行 Tomcat 启动。
    DispatcherServletAutoConfiguration ---》ServletWebServerFactoryAutoConfiguration  创建 tomcat。

  • 相关阅读:
    《七哥说道》第十八章:何处不风雨,套路说江湖
    《七哥说道》第十七章:北漂青年,人海茫茫
    《闲扯Redis四》List数据类型底层编码转换
    《闲扯Redis三》Redis五种数据类型之List型
    《闲扯Redis二》String数据类型之底层解析
    《闲扯Redis一》五种数据类型之String型
    Js解析Json数据获取元素JsonPath与深度
    《七哥说道》第十六章:程序员,江湖见
    Swagger2.9.2进入API界面报NumberFormatException异常
    绝顶高手必经之路【资源共享】
  • 原文地址:https://www.cnblogs.com/ming-blogs/p/11596870.html
Copyright © 2011-2022 走看看