SpringBoot 启动流程

启动入口

@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        // step.1
        SpringApplication.run(DemoApplication.class, args);
    }
}

实例化 SpringApplication 对象

// step.2
public static ConfigurableApplicationContext run(Class primarySource, String... args) {
    return run(new Class[]{primarySource}, args);
}

// step.3 
// 实例化SpringApplication对象,在构造方法内加载一些资源
public static ConfigurableApplicationContext run(Class[] primarySources, String[] args) {
    return new SpringApplication(primarySources).run(args);
}

// step.4
public SpringApplication(Class... primarySources) {
    this(null, primarySources);
}

加载运行类信息 & 确定应用类型 & 加载初始化器 & 加载监听器 & 设置运行主类

// step.5
@SuppressWarnings({ "unchecked", "rawtypes" })
public SpringApplication(ResourceLoader resourceLoader, Class... primarySources) {
    this.resourceLoader = resourceLoader;
    Assert.notNull(primarySources, "PrimarySources must not be null");
    // 加载运行类信息,此处的primarySources实际上是启动传入的class
    this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
    // 确定应用类型,默认servlet
    this.webApplicationType = WebApplicationType.deduceFromClasspath();
    this.bootstrapRegistryInitializers = new ArrayList<>(getSpringFactoriesInstances(BootstrapRegistryInitializer.class));
    // 加载初始化器
    setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
    // 加载监听器
    setListeners((Collection)getSpringFactoriesInstances(ApplicationListener.class));
    // 设置运行主类
    this.mainApplicationClass = deduceMainApplicationClass();
}

如果希望自定义初始化器,则需要实现 ApplicationContextInitializer 接口
加载希望自定义监听器,则需要实现 ApplicationListener 接口
以上两者都需要在resources目录下添加 META-INF/spring.factories 配置文件,将自定义的初始化器注册进去
SpringBoot 启动流程_第1张图片

执行启动方法 run()

// step.6
/**
 * Run the Spring application, creating and refreshing a new
 * {@link ApplicationContext}.
 * @param args the application arguments (usually passed from a Java main method)
 * @return a running {@link ApplicationContext}
 */
public ConfigurableApplicationContext run(String... args) {
    // 记录启动开始时间
    long startTime = System.nanoTime();
    DefaultBootstrapContext bootstrapContext = createBootstrapContext();
    // 声明应用上下文
    ConfigurableApplicationContext context = null;
    // 设置 无显示器依然启动
    configureHeadlessProperty();
    // 创建所有 Spring 运行监听器并发布应用启动事件 并启用监听器
    SpringApplicationRunListeners listeners = getRunListeners(args);
    listeners.starting(bootstrapContext, this.mainApplicationClass);
    try {
        // 封装应用参数
        ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
        // 环境准备
        ConfigurableEnvironment environment = prepareEnvironment(listeners, bootstrapContext, applicationArguments);
        configureIgnoreBeanInfo(environment);
        // 打印banner
        Banner printedBanner = printBanner(environment);
        // 创建应用上下文
        context = createApplicationContext();
        context.setApplicationStartup(this.applicationStartup);
        // 把相关的类放入上下文中
        prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);
        // 刷新上下文;
        // 将上述被加载的bean实例化;
        // @EnableAutoConfiguration 配置在此装配 
        // 启动tomcat
        refreshContext(context);
        // 刷新上下文后置处理
        afterRefresh(context, applicationArguments);
        // 记录启动完成时长
        Duration timeTakenToStartup = Duration.ofNanos(System.nanoTime() - startTime);
        if (this.logStartupInfo) {
            new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), timeTakenToStartup);
        }
        // 发布上下文准备就绪事件
        listeners.started(context, timeTakenToStartup);
        // 执行自定义的run方法
        callRunners(context, applicationArguments);
    }
    catch (Throwable ex) {
        handleRunFailure(context, ex, listeners);
        throw new IllegalStateException(ex);
    }
    try {
        Duration timeTakenToReady = Duration.ofNanos(System.nanoTime() - startTime);
        listeners.ready(context, timeTakenToReady);
    }
    catch (Throwable ex) {
        handleRunFailure(context, ex, null);
        throw new IllegalStateException(ex);
    }
    return context;
}

运行时监听器
SpringBoot 启动流程_第2张图片

扩展功能 callRunners(context, applicationArguments)
可以在启动完成后执行自定义的内容;有2种实现方式

  • 实现 ApplicationRunner 接口
  • 实现 CommandLineRunner 接口

参考资料:
https://blog.csdn.net/weixin_...

https://blog.csdn.net/w200921...

你可能感兴趣的:(SpringBoot 启动流程)