• SpringBoot启动过程


    SpringBoot启动过程

    • 创建 SpringApplication

      • 保存一些信息。

      • 判定当前应用的类型。ClassUtils。Servlet

      • bootstrappers:初始启动引导器(List):去spring.factories文件中找 org.springframework.boot.Bootstrapper

      • ApplicationContextInitializer;去spring.factoriesApplicationContextInitializer

      • List<ApplicationContextInitializer<?>> initializersimage-20210225170259036

      • ApplicationListener ;应用监听器。spring.factoriesApplicationListener

      • List<ApplicationListener<?>> listenersimage-20210225170436106

    • 运行 SpringApplicationimage-20210226152611735

      • 1. StopWatch

      • 2. 记录应用的启动时间

      • 3. 创建引导上下文(Context环境)createBootstrapContext()

        • 获取到所有之前的 bootstrappers 挨个执行 intitialize() 来完成对引导启动器上下文环境设置
      • 4. 让当前应用进入****headless模式。java.awt.headless

      • 5. 获取所有 RunListener(运行监听器)【为了方便所有Listener进行事件感知】

      • getSpringFactoriesInstances 去spring.factories找 SpringApplicationRunListener.image.png

        **遍历** **SpringApplicationRunListener 调用 starting 方法;**
        
      • 相当于通知所有感兴趣系统正在启动过程的人,项目正在 starting。

    • 6. 保存命令行参数;ApplicationArguments

    • 准备环境 prepareEnvironment();
      - 返回或者创建基础环境信息对象。StandardServletEnvironment
      - 配置环境信息对象。

      • 读取所有的配置源的配置属性值。
    • 绑定环境信息
      - 监听器调用 listener.environmentPrepared();通知所有的监听器当前环境准备完成

    • 7. 打印Banner图

    • 8. 创建IOC容器(createApplicationContext())

      • 根据项目类型(Servlet)创建容器,
      • 当前会创建 AnnotationConfigServletWebServerApplicationContext
    • 9. 准备ApplicationContext IOC容器的基本信息 prepareContext()

      • 保存环境信息

      • IOC容器的后置处理流程。

      • 应用初始化器;applyInitializers;

      • 遍历所有的 ApplicationContextInitializer 。调用 initialize.。来对ioc容器进行初始化扩展功能image.png

        • 遍历所有的 listener 调用 contextPrepared。EventPublishRunListenr;通知所有的监听器contextPrepared
      • 所有的监听器 调用 contextLoaded。通知所有的监听器 contextLoaded;

    • 10. 刷新IOC容器。refreshContext

      • 创建容器中的所有组件(Spring注解)
    • 11. 容器刷新完成后工作?afterRefresh

    • 12. StopWatch会显示用了多久

      • 所有监听 器 调用 listeners.started(context); 通知所有的监听器 started
    • 13 调用所有runners;callRunners()

    • 获取容器中的 ApplicationRunner
      - 获取容器中的 CommandLineRunner
      - 合并所有runner并且按照@Order进行排序
      - 遍历所有的runner。调用 run 方法

    • 如果以上有异常,

      • 调用Listener 的 failed
    • 调用所有监听器的 running 方法 listeners.running(context); 通知所有的监听器 running

    • running如果有问题。继续通知 failed 。调用所有 Listener 的 failed;通知所有的监听器 failed

    常见的5个重要的类

    /**
     * @author WGR
     * @create 2021/3/8 -- 13:25
     */
    public class MyApplicationContextInitializer implements ApplicationContextInitializer {
    
    
        @Override
        public void initialize(ConfigurableApplicationContext applicationContext) {
            System.out.println("MyApplicationContextInitializer ....initialize.... ");
        }
    }
    
    /**
     * @author WGR
     * @create 2021/3/8 -- 13:29
     */
    public class MyApplicationListener implements ApplicationListener {
        @Override
        public void onApplicationEvent(ApplicationEvent event) {
            System.out.println("MyApplicationListener.....onApplicationEvent...");
        }
    }
    
    /**
     * @author WGR
     * @create 2021/3/8 -- 13:32
     */
    @Component
    public class MyApplicationRunner implements ApplicationRunner {
        @Override
        public void run(ApplicationArguments args) throws Exception {
            System.out.println("MyApplicationRunner....run....");
        }
    }
    
    /**
     * @author WGR
     * @create 2021/3/8 -- 13:33
     */
    @Component
    public class MyCommandLineRunner implements CommandLineRunner {
        @Override
        public void run(String... args) throws Exception {
            System.out.println("MyCommandLineRunner....run....");
        }
    }
    
    
    /**
     * @author WGR
     * @create 2021/3/8 -- 13:30
     */
    public class MySpringApplicationRunListener implements SpringApplicationRunListener {
    
        private final SpringApplication application;
    
        public MySpringApplicationRunListener(SpringApplication application, String[] args) {
            this.application = application;
        }
    
        @Override
        public void starting(ConfigurableBootstrapContext bootstrapContext) {
            System.out.println("MySpringApplicationRunListener....starting....");
    
        }
    
    
        @Override
        public void environmentPrepared(ConfigurableBootstrapContext bootstrapContext, ConfigurableEnvironment environment) {
            System.out.println("MySpringApplicationRunListener....environmentPrepared....");
        }
    
    
        @Override
        public void contextPrepared(ConfigurableApplicationContext context) {
            System.out.println("MySpringApplicationRunListener....contextPrepared....");
    
        }
    
        @Override
        public void contextLoaded(ConfigurableApplicationContext context) {
            System.out.println("MySpringApplicationRunListener....contextLoaded....");
        }
    
        @Override
        public void started(ConfigurableApplicationContext context) {
            System.out.println("MySpringApplicationRunListener....started....");
        }
    
        @Override
        public void running(ConfigurableApplicationContext context) {
            System.out.println("MySpringApplicationRunListener....running....");
        }
    
        @Override
        public void failed(ConfigurableApplicationContext context, Throwable exception) {
            System.out.println("MySpringApplicationRunListener....failed....");
        }
    }
    

    注意:如果MySpringApplicationRunListener类没有构造器的话,会报下面这个错

    image-20210308134408254

    运行的周期如下:

    MyApplicationListener.....onApplicationEvent...
    MySpringApplicationRunListener....starting....
    MyApplicationListener.....onApplicationEvent...
    MySpringApplicationRunListener....environmentPrepared....
    
      .   ____          _            __ _ _
     /\ / ___'_ __ _ _(_)_ __  __ _    
    ( ( )\___ | '_ | '_| | '_ / _` |    
     \/  ___)| |_)| | | | | || (_| |  ) ) ) )
      '  |____| .__|_| |_|_| |_\__, | / / / /
     =========|_|==============|___/=/_/_/_/
     :: Spring Boot ::                (v2.4.2)
    
    MyApplicationContextInitializer ....initialize.... 
    MyApplicationListener.....onApplicationEvent...
    MySpringApplicationRunListener....contextPrepared....
    2021-03-08 13:47:46.528  INFO 7736 --- [           main] com.dalianpai.admin.AdminApplication     : Starting AdminApplication using Java 1.8.0_131 on WGR-PC with PID 7736 (E:IdeaSpacedemospring-boot-admin	argetclasses started by asus in E:IdeaSpacedemo)
    2021-03-08 13:47:46.532  INFO 7736 --- [           main] com.dalianpai.admin.AdminApplication     : No active profile set, falling back to default profiles: default
    MyApplicationListener.....onApplicationEvent...
    MySpringApplicationRunListener....contextLoaded....
    2021-03-08 13:47:48.519  INFO 7736 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8888 (http)
    2021-03-08 13:47:48.528  INFO 7736 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
    2021-03-08 13:47:48.528  INFO 7736 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.41]
    2021-03-08 13:47:48.622  INFO 7736 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
    2021-03-08 13:47:48.622  INFO 7736 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1969 ms
    2021-03-08 13:47:48.871  INFO 7736 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
    2021-03-08 13:47:48.972  WARN 7736 --- [           main] ion$DefaultTemplateResolverConfiguration : Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)
    2021-03-08 13:47:49.366  INFO 7736 --- [           main] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 2 endpoint(s) beneath base path '/actuator'
    2021-03-08 13:47:49.403  INFO 7736 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8888 (http) with context path ''
    MyApplicationListener.....onApplicationEvent...
    MyApplicationListener.....onApplicationEvent...
    2021-03-08 13:47:49.417  INFO 7736 --- [           main] com.dalianpai.admin.AdminApplication     : Started AdminApplication in 3.504 seconds (JVM running for 6.046)
    MyApplicationListener.....onApplicationEvent...
    MyApplicationListener.....onApplicationEvent...
    MySpringApplicationRunListener....started....
    MyApplicationRunner....run....
    MyCommandLineRunner....run....
    MyApplicationListener.....onApplicationEvent...
    MyApplicationListener.....onApplicationEvent...
    MySpringApplicationRunListener....running....
    
  • 相关阅读:
    HTML5 拖放(Drag 和 Drop)详解与实例
    JS中的六大数据类型
    关于创建本地docker仓库
    关于使用国内dock仓库,网易、DaoCloud
    关于Docker开通远程访问端口2375
    多个消费者监听同一个队列
    SQLite -附加数据库
    解密JDK8 枚举
    LoraLU
    深入理解display属性
  • 原文地址:https://www.cnblogs.com/dalianpai/p/14499702.html
Copyright © 2020-2023  润新知