SpringBoot监听器模式实例分析
1、事件ApplicationEventApplicationEvent是一个抽象类,idea上展开其继承关系如图:可见SpringBoot所定义的事件类型是极为丰富的。2、监听器ApplicationListenerApplicationListener是一个接口,我们也可以通过实现这个接口来定义自己的监听器,可以通过与事件初始化器方式相似的方式进行加载。@FunctionalInterfacepublicinterfaceApplicationListenerextendsEventListener{
1、事件ApplicationEvent
ApplicationEvent是一个抽象类,idea上展开其继承关系如图:

可见SpringBoot所定义的事件类型是极为丰富的。
2、监听器ApplicationListener
ApplicationListener是一个接口,我们也可以通过实现这个接口来定义自己的监听器,可以通过与事件初始化器方式相似的方式进行加载。
@FunctionalInterface public interface ApplicationListenerextends EventListener { /** * Handle an application event. * @param event the event to respond to */ void onApplicationEvent(E event); }
我们可以看到代码中它接受一个上文中提到的事件泛型,这代表了此监听器关注的事件;
还有一种实现监听器的方式,即实现SmartApplicationListener接口,SmartApplicationListener继承了ApplicationListener接口,通过这种方式实现监听器,可以同时注册多个感兴趣的事件,只需实现接口的supportsEventType方法即可;
public interface SmartApplicationListener extends ApplicationListener, Ordered { /** * Determine whether this listener actually supports the given event type. * @param eventType the event type (never {@code null}) */ boolean supportsEventType(Class extends ApplicationEvent> eventType); /** * Determine whether this listener actually supports the given source type. * The default implementation always returns {@code true}. * @param sourceType the source type, or {@code null} if no source */ default boolean supportsSourceType(@Nullable Class> sourceType) { return true; } /** * Determine this listener's order in a set of listeners for the same event. *
The default implementation returns {@link #LOWEST_PRECEDENCE}. */ @Override default int getOrder() { return LOWEST_PRECEDENCE; } }
3、事件广播器ApplicationEventMulticaster
ApplicationEventMulticaster是一个接口,定义了添加监听器、删除监听器、传播事件等方法;
SpringBoot为我们实现了SimpleApplicationEventMulticaster这一事件广播器,继承关系如图所示:

Windows 10 是一款微软推出的经典操作系统,拥有硬件兼容性与多任务处理能力。它更偏向把系统状态查看和常用调节动作放在一起,适合需要持续观察和微调设备状态的场景。
极度公式是一款跨平台专业LaTeX公式识别编辑软件,支持OCR公式识别和多平台编辑。和使用说明,避免使用,享受完整功能与稳定支持。做扫描整理、文字提取和表格转换时,它能把识别后的处理步骤接得更顺,资料录入这类场景会省下不少时间。
















