SpringBoot内置生命周期事件详解 SpringBoot源码(十)

SpringBoot中文注释项目Github地址:

https://github.com/yuanmabiji/spring-boot-2.1.0.RELEASE

本篇接 SpringBoot事件监听机制源码分析(上) SpringBoot源码(九)

1 温故而知新

温故而知新,我们来简单回顾一下上篇的内容,上一篇我们分析了SpringBoot启动时广播生命周期事件的原理,现将关键步骤再浓缩总结下:

  1. 为广播SpringBoot内置生命周期事件做前期准备:1)首先加载ApplicationListener监听器实现类;2)其次加载SPI扩展类EventPublishingRunListener
  2. SpringBoot启动时利用EventPublishingRunListener广播生命周期事件,然后ApplicationListener监听器实现类监听相应的生命周期事件执行一些初始化逻辑的工作。

2 引言

上篇文章的侧重点是分析了SpringBoot启动时广播生命周期事件的原理,此篇文章我们再来详细分析SpringBoot内置的7种生命周期事件的源码。

3 SpringBoot生命周期事件源码分析

分析SpringBoot的生命周期事件,我们先来看一张类结构图:

由上图可以看到事件类之间的关系:

  1. 最顶级的父类是JDK的事件基类EventObject
  2. 然后Spring的事件基类ApplicationEvent继承了JDK的事件基类EventObject
  3. 其次SpringBoot的生命周期事件基类SpringApplicationEvent继承了Spring的事件基类ApplicationEvent
  4. 最后SpringBoot具体的7个生命周期事件类再继承了SpringBoot的生命周期事件基类SpringApplicationEvent

3.1 JDK的事件基类EventObject

EventObject类是JDK的事件基类,可以说是所有Java事件类的基本,即所有的Java事件类都直接或间接继承于该类,源码如下:

// EventObject.java

public class EventObject implements java.io.Serializable {
   

    private static final long serialVersionUID = 5516075349620653480L;

    /**
     * The object on which the Event initially occurred.
     */
    protected transient Object  source;
    /**
     * Constructs a prototypical Event.
     *
     * @param    source    The object on which the Event initially occurred.
     * @exception  IllegalArgumentException  if source is null.
     */
    public EventObject(Object source) {
   
        if (source == null)
            throw new IllegalArgumentException("null source");
        this.source = source;
    }
    /**
     * The object on which the Event initially occurred.
     *
     * @return   The object on which the Event initially occurred.
     */
    public Object getSource() {
   
        return source;
    }
    /**
     * Returns a String representation of this EventObject.
     *
     * @return  A a String representation of this EventObject.
     */
    public String toString() {
   
        return getClass().getName() + "[source=" + source + "]";
    }
}

可以看到EventObject类只有一个属性source,这个属性是用来记录最初事件是发生在哪个类,举个栗子,比如在SpringBoot启动过程中会发射ApplicationStartingEvent事件,而这个事件最初是在SpringApplication类中发射的,因此source就是SpringApplication对象。

3.2 Spring的事件基类ApplicationEvent

ApplicationEvent继承了DK的事件基类EventObject类,是Spring的事件基类,被所有Spring的具体事件类继承,源码如下:

// ApplicationEvent.java

/**
 * Class to be extended by all application events. Abstract as it
 * doesn't make sense for generic events to be published directly.
 *
 * @author Rod Johnson
 * @author Juergen Hoeller
 */
public abstract class ApplicationEvent extends EventObject {
   
	/** use serialVersionUID from Spring 1.2 for interoperability. */
	private static final long serialVersionUID 
  • 47
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值