zoukankan      html  css  js  c++  java
  • 事件监听机制

    启动流程:

    1、创建SpringApplication对象

    private void initialize(Object[] sources) {
            if (sources != null && sources.length > 0) {
                this.sources.addAll(Arrays.asList(sources));//保存主配置类
            }
            this.webEnvironment = deduceWebEnvironment();
            setInitializers((Collection) getSpringFactoriesInstances(
                    ApplicationContextInitializer.class));//从类路径下找到META‐INF/spring.factories配置的所有ApplicationContextInitializer;然后保存起来
            setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
        //从类路径下找到ETA‐INF/spring.factories配置的所有ApplicationListener
    this.mainApplicationClass = deduceMainApplicationClass();//从多个配置类中找到有main方法的主配置类 }

    2、运行run方法

    public ConfigurableApplicationContext run(String... args) {
            StopWatch stopWatch = new StopWatch();
            stopWatch.start();
            ConfigurableApplicationContext context = null;
            FailureAnalyzers analyzers = null;
            configureHeadlessProperty();
            SpringApplicationRunListeners listeners = getRunListeners(args);
        //获取SpringApplicationRunListeners;从类路径下META‐INF/spring.factories listeners.starting();
        //回调所有的获取SpringApplicationRunListener.starting()方法
    try { ApplicationArguments applicationArguments = new DefaultApplicationArguments( args); ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
          
    //创建环境完成后回调SpringApplicationRunListener.environmentPrepared();表示环境准备完成
                Banner printedBanner = printBanner(environment);
                context = createApplicationContext();
                analyzers = new FailureAnalyzers(context);

            //准备上下文环境;将environment保存到ioc中;而且applyInitializers();
            //applyInitializers():回调之前保存的所有的ApplicationContextInitializer的initialize方法
            //回调所有的SpringApplicationRunListener的contextPrepared();

                prepareContext(context, environment, listeners, applicationArguments,
                        printedBanner);
          
          
    //prepareContext运行完成以后回调所有的SpringApplicationRunListener的contextLoaded()

           refreshContext(context);

            //从ioc容器中获取所有的ApplicationRunner和CommandLineRunner进行回调
            //ApplicationRunner先回调,CommandLineRunner再回调

           afterRefresh(context, applicationArguments);

          
    //所有的SpringApplicationRunListener回调finished方法 listeners.finished(context, null); stopWatch.stop(); if (this.logStartupInfo) { new StartupInfoLogger(this.mainApplicationClass) .logStarted(getApplicationLog(), stopWatch); } return context; } catch (Throwable ex) { handleRunFailure(context, listeners, analyzers, ex); throw new IllegalStateException(ex); } }

    自定义ApplicationContextInitializer继承ApplicationContextInitializer<ConfigurableApplicationContext>

    自定义SpringApplicationRunListener继承SpringApplicationRunListener

    配置在Resources(META-INF/spring.factories)如:

    org.springframework.context.ApplicationContextInitializer=com.atguigu.springboot.listener.HelloApplicationContextInitializer
    org.springframework.boot.SpringApplicationRunListener=com.atguigu.springboot.listener.HelloSpringApplicationRunListener

    自定义ApplicationRunner、CommandLineRunner只需放入IOC 容器即可。

  • 相关阅读:
    分支可以类型的多态实现
    c#中使用了using自动释放资源,如果在using中被return或者异常终止,也会继续执行dispose函数的
    C# 数组的Clone
    pg_dump备份数据结构及数据
    SpringBoot使用MockMVC单元测试Controller
    Docker连接私服
    Windows安装Docker
    项目启动报错Failed to configure a DataSource: 'url' attribute is not specified and no embedde
    剑指Offer对答如流系列
    剑指Offer对答如流系列
  • 原文地址:https://www.cnblogs.com/xiaoliangup/p/10420790.html
Copyright © 2011-2022 走看看