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

  • 相关阅读:
    [Memcache] memcache中的过期时间策略
    [Memcache] memcache中add和set方法的区别
    [Redis] redis业务实践 , 这次用哈希
    [PHP] PHPMailer发信失败,用这种方式找原因
    [PHP] fastcgi_split_path_info与传递PATH_INFO
    [PHP] 设计一个可扩展的用户登陆系统
    [OAuth] OAuth2.0中的客户端模式
    [Linux] 解决CentOS下Requires: libjson-c.so错误
    [PHP] PHP5中的写时复制change on write
    [JavaScript]js中typeof的用法
  • 原文地址:https://www.cnblogs.com/xiaoliangup/p/10420790.html
Copyright © 2011-2022 走看看