spring-boot event

spring-boot 事件模型: event - listenter

event

public abstract class ApplicationEvent extends EventObject {

    /** use serialVersionUID from Spring 1.2 for interoperability */
    private static final long serialVersionUID = 7099057708183571937L;

    /** System time when the event happened */
    private final long timestamp;


    /**
     * Create a new ApplicationEvent.
     * @param source the object on which the event initially occurred (never {@code null})
     */
    public ApplicationEvent(Object source) {
        super(source);
        this.timestamp = System.currentTimeMillis();
    }


    /**
     * Return the system time in milliseconds when the event happened.
     */
    public final long getTimestamp() {
        return this.timestamp;
    }
}

ApplicationEvent 抽象类继承JDK的EventObject,source即数据源。
spring-boot 事件依赖于抽象类 SpringApplicationEvent,而SpringApplicationEvent继承ApplicationEvent。SpringApplicationEvent有以下几种子类事件:

  • ApplicationEnvironmentPreparedEvent : 当{@linkSpringApplication}启动时和 {@link Environment}首次检查和修改时发布该事件

  • ApplicationFailedEvent : 当{@linkSpringApplication}启动失败时发布该事件

  • ApplicationPreparedEvent :当{@linkSpringApplication}启动时并且{@link ApplicationContext} 上下文已准备好但没有refresh时发布该事件。此时bean definitions 将要被加载,{@link Environment} 系统环境已可被使用。

  • ApplicationReadyEvent : ?

  • ApplicationStartingEvent:{@link SpringApplication} 启动时且{@link Environment} 和{@link ApplicationContext} 还未初始化

EventPublishingRunListener 是spring-boot 的事件监听者,实现了SpringApplicationRunListener和Ordered 接口。看下它的构造函数:

    public EventPublishingRunListener(SpringApplication application, String[] args) {
        this.application = application;
        this.args = args;
        this.initialMulticaster = new SimpleApplicationEventMulticaster();
        for (ApplicationListener<?> listener : application.getListeners()) {
            this.initialMulticaster.addApplicationListener(listener);
        }
    }

其核心是初始一个SimpleApplicationEventMulticaster 简单实现的spring事件广播,并将SpringApplication中的事件事件监听者添加至广播器中。

SpringApplicationRunListener 是SpringApplication调用run 方法的事件监听者,其下包含是SpringApplication启动时各种事件监听。

/**
     * Called immediately when the run method has first started. Can be used for very
     * early initialization.
     */
    void starting();

    /**
     * Called once the environment has been prepared, but before the
     * {@link ApplicationContext} has been created.
     * @param environment the environment
     */
    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
     */
    void contextPrepared(ConfigurableApplicationContext context);

    /**
     * Called once the application context has been loaded but before it has been
     * refreshed.
     * @param context the application context
     */
    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.
     */
    void finished(ConfigurableApplicationContext context, Throwable exception);

详细的调用链过程可查看new SpringApplication()时的SpringFactoriesLoader监听者添加与及SpringApplication.run()时的listeners.starting()。

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

推荐阅读更多精彩内容