• springboot启动流程


    SpringApplication.run(AppRun.class, args);
    
    1. public ConfigurableApplicationContext run(String... args) {
    2. StopWatch stopWatch = new StopWatch();
    3. stopWatch.start();
    4. ConfigurableApplicationContext context = null;
    5. Collection exceptionReporters = new ArrayList();
    6. this.configureHeadlessProperty();
    7. SpringApplicationRunListeners listeners = this.getRunListeners(args);
    8. listeners.starting();
    9. Collection exceptionReporters;
    10. try {
    11. ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
    12. ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);
    13. this.configureIgnoreBeanInfo(environment);
    14. Banner printedBanner = this.printBanner(environment);
    15. context = this.createApplicationContext();
    16. exceptionReporters = this.getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[]{ConfigurableApplicationContext.class}, context);
    17. this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);
    18. this.refreshContext(context);
    19. this.afterRefresh(context, applicationArguments);
    20. stopWatch.stop();
    21. if (this.logStartupInfo) {
    22. (new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch);
    23. }
    24. listeners.started(context);
    25. this.callRunners(context, applicationArguments);
    26. } catch (Throwable var10) {
    27. this.handleRunFailure(context, var10, exceptionReporters, listeners);
    28. throw new IllegalStateException(var10);
    29. }
    30. try {
    31. listeners.running(context);
    32. return context;
    33. } catch (Throwable var9) {
    34. this.handleRunFailure(context, var9, exceptionReporters, (SpringApplicationRunListeners)null);
    35. throw new IllegalStateException(var9);
    36. }
    37. }
    1. 将当前启动类的字节码传入(主要目的是传入@SpringBootApplication这个注解), 以及main函数的args参数.
    2. 开启计时器

    StopWatch stopWatch = new StopWatch();
            stopWatch.start();

         3.声明context容器

            ConfigurableApplicationContext context = null;
            Collection exceptionReporters = new ArrayList();
            this.configureHeadlessProperty();

        4.创建并启动主入口的监听器

            SpringApplicationRunListeners listeners = this.getRunListeners(args);
            listeners.starting();

      5. 解析应用程序的启动参数

    ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);

    6.创建并配置启动环境(加载配置文件) 

    ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);

    7. 排除不需要的环境

    this.configureIgnoreBeanInfo(environment);

    8.输出banner 

    Banner printedBanner = this.printBanner(environment);

    9.根据webApplicationType创建应用上下文

    context = this.createApplicationContext();

    protected ConfigurableApplicationContext createApplicationContext() {
        Class contextClass = this.applicationContextClass;
        if (contextClass == null) {
            try {
                switch(this.webApplicationType) {
                case SERVLET:
                    contextClass = Class.forName("org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext");
                    break;
                case REACTIVE:
                    contextClass = Class.forName("org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext");
                    break;
                default:
                    contextClass = Class.forName("org.springframework.context.annotation.AnnotationConfigApplicationContext");
                }
            } catch (ClassNotFoundException var3) {
                throw new IllegalStateException("Unable create a default ApplicationContext, please specify an ApplicationContextClass", var3);
            }
        }
    
        return (ConfigurableApplicationContext)BeanUtils.instantiateClass(contextClass);
    }

    10. 获取类加载器,加载spring.factories的全类名集合,反射获取class创建实例对象,并根据优先级返回列表

                exceptionReporters = this.getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[]{ConfigurableApplicationContext.class}, context);

    private  Collection getSpringFactoriesInstances(Class type, Class[] parameterTypes, Object... args) {
        ClassLoader classLoader = this.getClassLoader();
        Set names = new LinkedHashSet(SpringFactoriesLoader.loadFactoryNames(type, classLoader));
        List instances = this.createSpringFactoriesInstances(type, parameterTypes, classLoader, args, names);
        AnnotationAwareOrderComparator.sort(instances);
        return instances;
    }

    11.预处理上下文

     this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);

     这一步主要是在容器刷新之前的准备动作。设置容器环境,包括各种变量等等,其中包含一个非常关键的操作:将启动类注入容器,为后续开启自动化配置奠定基础

    12.刷新上下文

    this.refreshContext(context);

    开启刷新spring容器,通过refresh方法对整个IOC容器的初始化(包括bean资源的定位,解析,注册等等),同时向JVM运行时注册一个关机钩子,在JVM关机时会关闭这个上下文,除非当时它已经关闭

    13.空方法

    this.afterRefresh(context, applicationArguments);

    protected void afterRefresh(ConfigurableApplicationContext context, ApplicationArguments args) {
    }

     扩展接口,设计模式中的模板方法,默认为空实现。如果有自定义需求,可以重写该方法。比如打印一些启动结束log,或者一些其它后置处理。

    14.停止计时器

    stopWatch.stop();

    15. 

    获取EventPublishingRunListener监听器,并执行其started方法,并且将创建的Spring容器传进去了,创建一个ApplicationStartedEvent事件,并执行ConfigurableApplicationContext 的publishEvent方法,也就是说这里是在Spring容器中发布事件,并不是在SpringApplication中发布事件,和前面的starting是不同的,前面的starting是直接向SpringApplication中的监听器发布启动事件。

              listeners.started(context)

    16. 

    用于调用项目中自定义的执行器XxxRunner类,使得在项目启动完成后立即执行一些特定程序。其中,Spring Boot提供的执行器接口有ApplicationRunner 和CommandLineRunner两种,在使用时只需要自定义一个执行器类实现其中一个接口并重写对应的run()方法接口,然后Spring Boot项目启动后会立即执行这些特定程序

      this.callRunners(context, applicationArguments);

  • 相关阅读:
    Linux之Shell(二)
    JavaEE——No.2 套接字编程(TCP)
    jdk21本地执行flink出现不兼容问题
    vue列表导出word文档
    LeetCode75——Day30
    Quartz.NET,强大的开源作业调度框架
    s3fs挂载多个桶或普通多目录通过NFS共享踩坑
    CCRC-DSA数据安全评估师:加快构建大网络安全工作格局
    day1- day6
    缓存之缓存简介
  • 原文地址:https://blog.csdn.net/xlj778/article/details/127863951