springboot事件监听

看那个人好像一条狗-------------------------------------By 周星星
首先来看事件监听的定义的SpringApplicationRunListener,用户可以通过这个接口实现自己的自定义流程在SpringApplication启动过程中,现在我们来看看SpringApplicationRunListener的主要结构,这个接口主要定义了5个方法,包含了SpringApplication启动流程一系列事件


import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.io.support.SpringFactoriesLoader;

/**
 * Listener for the {@link SpringApplication} {@code run} method.
 * {@link SpringApplicationRunListener}s are loaded via the {@link SpringFactoriesLoader}
 * and should declare a public constructor that accepts a {@link SpringApplication}
 * instance and a {@code String[]} of arguments. A new
 * {@link SpringApplicationRunListener} instance will be created for each run.
 *
 * @author Phillip Webb
 * @author Dave Syer
 */
public interface SpringApplicationRunListener {

    /**
     * Called immediately when the run method has first started. Can be used for very
     * early initialization.
     *SpringApplication运行run方法时立即调用此方法,可以用户非常早期的初始化工作
     */

    void starting();

    /**
     * Called once the environment has been prepared, but before the
     * {@link ApplicationContext} has been created.
     * @param environment the environment
     * Environment准备好后,并且ApplicationContext创建之前调用
     */
    void environmentPrepared(ConfigurableEnvironment environment);

    /**
     * Called once the {@link ApplicationContext} has been created and prepared, but
     * before sources have been loaded.
     * @param context the application context
     * ApplicationContext创建好后立即调用
     */
    void contextPrepared(ConfigurableApplicationContext context);

    /**
     * Called once the application context has been loaded but before it has been
     * refreshed.
     * @param context the application context
     * ApplicationContext加载完成,在refresh之前调用
     */
    void contextLoaded(ConfigurableApplicationContext context);

    /**
     * Called immediately before the run method finishes.
     * @param context the application context or null if a failure occurred before the
     * context was created
     * @param exception any run exception or null if run completed successfully.
     * 当run方法结束之前调用
     */
    void finished(ConfigurableApplicationContext context, Throwable exception);

}

SpringBoot通过读取spring.factories文件通过反射的机制获取SpringApplicationRunListener的实现类

/**
     * Run the Spring application, creating and refreshing a new
     * {@link ApplicationContext}.
     * @param args the application arguments (usually passed from a Java main method)
     * @return a running {@link ApplicationContext}
     */
    public ConfigurableApplicationContext run(String... args) {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        ConfigurableApplicationContext context = null;
        Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
        configureHeadlessProperty();
        SpringApplicationRunListeners listeners = getRunListeners(args);
        listeners.starting();
        try {
            ApplicationArguments applicationArguments = new DefaultApplicationArguments(
                    args);
            ConfigurableEnvironment environment = prepareEnvironment(listeners,
                    applicationArguments);
            configureIgnoreBeanInfo(environment);
            Banner printedBanner = printBanner(environment);
            context = createApplicationContext();
            exceptionReporters = getSpringFactoriesInstances(
                    SpringBootExceptionReporter.class,
                    new Class[] { ConfigurableApplicationContext.class }, context);
            prepareContext(context, environment, listeners, applicationArguments,
                    printedBanner);
            refreshContext(context);
            afterRefresh(context, applicationArguments);
            listeners.finished(context, null);
            stopWatch.stop();
            if (this.logStartupInfo) {
                new StartupInfoLogger(this.mainApplicationClass)
                        .logStarted(getApplicationLog(), stopWatch);
            }
            callRunners(context, applicationArguments);
            return context;
        }
        catch (Throwable ex) {
            handleRunFailure(context, listeners, exceptionReporters, ex);
            throw new IllegalStateException(ex);
        }
    }


其中SpringApplicationRunListeners listeners = getRunListeners(args)就是springBoot获取SpringApplicationRunListener监听器核心代码,时序图如下

springbootlistener.png

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 时间飞逝,光阴如梭,回顾即将过去的2018年,是辛苦的一年、欢乐的一年、付出的一年、也是收获的一年。在我眼里,设计...
    婷子_3a24阅读 9,526评论 0 0
  • 不知别人是否跟我一样,孩提时代,总会跟自己的影子过不去,常常坐在山坡上猜想,影子是不是被赋予某种使命,令我拥有超能...
    Gigi_qin阅读 569评论 0 0
  • 平静有两个维度,对人事,对自己。真正的平静来自于认清自己,认清了自己也就认清了别人。在这个意义上说,平静是...
    冰夫阅读 247评论 0 0
  • 校区:科学创想机器人和平校区 班级:周六10:40-11:40 学员:杜沈轩,杜辽轩 任课教师:杨玲 教学目标: ...
    Happy00阅读 203评论 0 0