可能是全网最全的SpringBoot启动流程源码分析(基于 2.1.5 版本)

使用 Spring Boot 启动一个微服务十分简单,只需要在启动类上调用 SpringApplication 的run方法即可

点击进入run方法

1 run

静态辅助类,可用于运行使用默认配置(即我们添加的一系列注解)的指定源的 SpringApplication 。

  • primarySource - 要载入的主要源,即指定源,这里为传入的Application.class
    Class<?> :泛型决定了任何类都可以传入
  • args - 应用程序参数(通常是从main方法传递)
  • 返回:正在运行的ApplicationContext

上面是 SpringApplication实例对象构造方法初始化过程
继续看这个 SpringApplication 对象的 run 方法的源码和运行流程

public ConfigurableApplicationContext run(String... args) {    
   
    // 1 创建并启动计时监控类
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();    

    // 2 初始化应用上下文和异常报告集合
    ConfigurableApplicationContext context = null;
    Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();    

    // 3 设置系统属性 java.awt.headless 的值,默认为true
    configureHeadlessProperty();    

    // 4、创建所有 Spring 运行监听器并发布应用启动事件
    SpringApplicationRunListeners listeners = getRunListeners(args);
    listeners.starting();    
    try {        // 5、初始化默认应用参数类
        ApplicationArguments applicationArguments = new DefaultApplicationArguments(
                args);                
        // 6、根据运行监听器和应用参数来准备 Spring 环境
        ConfigurableEnvironment environment = prepareEnvironment(listeners,
                applicationArguments);
        configureIgnoreBeanInfo(environment);        
        // 7、创建 Banner 打印类
        Banner printedBanner = printBanner(environment);        
        // 8、创建应用上下文
        context = createApplicationContext();        
        // 9、准备异常报告器
        exceptionReporters = getSpringFactoriesInstances(
                SpringBootExceptionReporter.class,                new Class[] { ConfigurableApplicationContext.class }, context);                
        // 10、准备应用上下文
        prepareContext(context, environment, listeners, applicationArguments,
                printedBanner);                
        // 11、刷新应用上下文
        refreshContext(context);        
        // 12、应用上下文刷新后置处理
        afterRefresh(context, applicationArguments);        
        // 13、停止计时监控类
        stopWatch.stop();        
        // 14、输出日志记录执行主类名、时间信息
        if (this.logStartupInfo) {            new StartupInfoLogger(this.mainApplicationClass)
                    .logStarted(getApplicationLog(), stopWatch);
        }        
        // 15、发布应用上下文启动完成事件
        listeners.started(context);        
        // 16、执行所有 Runner 运行器
        callRunners(context, applicationArguments);
    }    catch (Throwable ex) {
        handleRunFailure(context, ex, exceptionReporters, listeners);        throw new IllegalStateException(ex);
    }    try {        // 17、发布应用上下文就绪事件
        listeners.running(context);
    }    catch (Throwable ex) {
        handleRunFailure(context, ex, exceptionReporters, null);        throw new IllegalStateException(ex);
    }    
    // 18、返回应用上下文
    return context;
}

1 创建并启动计时监控类

  • 类概述
  • 首先记录了当前任务的名称,默认为空串,然后记录当前 Spring Boot 应用启动的开始时间
    StopWatch#

设置系统属性 java.awt.headless 的值


SpringApplication#

对于一个 Java 服务器来说经常要处理一些图形元素,例如地图的创建或者图形和图表等。这些API基本上总是需要运行一个X-server以便能使用AWT(Abstract Window Toolkit,抽象窗口工具集)。然而运行一个不必要的 X-server 并不是一种好的管理方式。有时你甚至不能运行 X-server,因此最好的方案是运行 headless 服务器,来进行简单的图像处理。

参考:www.cnblogs.com/princessd8251/p/4000016.html

创建所有 Spring 运行监听器并发布应用启动事件

调用getSpringFactoriesInstances 来获取配置的监听器名称并实例化所有的类

SpringApplicationRunListener所有监听器配置在spring-boot-2.0.3.RELEASE.jar!/META-INF/spring.factories这个配置文件里面。



初始化默认应用参数类



发布了470 篇原创文章 · 获赞 1541 · 访问量 70万+

猜你喜欢

转载自blog.csdn.net/qq_33589510/article/details/105755279