• 事件监听机制


    启动流程:

    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 容器即可。

  • 相关阅读:
    linux命令--cp
    linux命令--mv
    CSS属性值定义语法中的符号说名
    select选项改变时获取选中的option的值
    JS截取字符串:slice(),substring()和substr()
    正则表达式进行密码验证
    利用@media实现IE hack
    javascript版1024游戏源码
    canvas写的一个小时钟demo
    gl.vertexAtteib3f P42 讲数据传给location参数指定的attribute变量
  • 原文地址:https://www.cnblogs.com/xiaoliangup/p/10420790.html
Copyright © 2020-2023  润新知