Spring源码系列(3)―ApplicationListener

系列文章目录

提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加
例如:第一章 Python 机器学习入门之pandas的使用


提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

提示:这里可以添加本文要记录的大概内容:
例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。


参考连接

https://blog.csdn.net/weixin_43767015/article/details/110265205
https://zhuanlan.zhihu.com/p/378927813

一、监听器模式

观察者模式的再度抽象
Spring中观察者模式包含四个角色:事件、事件监听器、事件源、事件管理。

二、Spring中的应用

  ApplicationListener是Spring事件机制的一部分,与抽象类ApplicationEvent类配合来完成ApplicationContext的事件机制。   如果容器中存在ApplicationListener的Bean,当ApplicationContext调用publishEvent方法时,对应的Bean会被触发。这一过程是典型的观察者模式的实现。

1.事件/应用程序事件
  ApplicationEvent是所有事件对象的父类。ApplicationEvent继承自jdk的EventObject,所有的事件都需要继承ApplicationEvent,并且通过source得到事件源。Spring 提供了很多内置事件,比如:ContextRefreshedEvent、ContextStartedEvent、ContextStoppedEvent、ContextClosedEvent、RequestHandledEvent,所有应用程序事件都扩展这个类,它是事件源和事件监听器的内部桥梁。继承了位于JDK核心rt.jar包中的Java的事件基类EventObject。

2.事件监听器
  ApplicationListener,也就是观察者,继承自jdk的EventListener,该类中只有一个方法onApplicationEvent,当监听的事件发生后该方法会被执行。

3.事件源/应用程序事件发布者
  ApplicationEventPublisher,ApplicationContext是Spring的核心容器,在事件监听中ApplicationContext可以作为事件的发布者,也就是事件源。因为ApplicationContext继承自ApplicationEventPublisher。在ApplicationEventPublisher中定义了事件发布的方法:publishEvent(Object event)。

4.事件管理/应用事件广播器
  ApplicationEventMulticaster,用于事件监听器的注册和事件的广播。监听器的注册就是通过它来实现的,它的作用是把Applicationcontext发布的Event广播给它的监听器列表。
  
  
  自 Spring 4.2 起,Spring事件发布机制得到显著增强,可以通过ApplicationEventPublisher发布任意自定义事件,并且事件不必再继承ApplicationEvent,它们自动包装为一个事件,并且还可以通过@EventListener注解监听事件。
  Spring提供了一系列标准事件,也就是一系列容器状态相关的应用程序事件,它们都继承了ApplicationEvent。

Event描述
ContextRefreshedEvent上下文刷新事件。在ApplicationContext被初始化或者刷新完毕之后调用,即ConfigurableApplicationContext.refresh()方法尾部的publishEvent方法就会发布该类型事件。该事件发布之后,标志着容器初始化或者刷新完毕,所有的非延迟初始化的单例bean已被加载,后处理器bean都已回调完毕,上下文容易已准备好被使用。
ContextStartedEvent上下文启动事件。在ApplicationContext被启动之后调用,即ConfigurableApplicationContext.start()方法尾部的publishEvent方法就会发布该类型事件。该事件发布之后,标志着所有的Lifecycle包括SmartLifecycle已被启动(调用start()方法),
ContextStoppedEvent上下文停止事件。在ApplicationContext被停止之后调用,即ConfigurableApplicationContext.stop()方法尾部的publishEvent方法就会发布该类型事件。该事件发布之后,标志着所有的Lifecycle包括SmartLifecycle已被停止(调用stop()方法)。被停止的上下文可通过start()重新启动。
ContextClosedEvent上下文关闭事件。在ApplicationContext被关闭之后调用,即ConfigurableApplicationContext.close()方法尾部的publishEvent方法就会发布该类型事件。该事件发布之后,所有单例bean将被销毁(并调用销毁回调),所有的Lifecycle将被停止(执行stop()方法)。容器销毁,无法刷新或重新启动。
RequestHandledEvent请求处理事件。仅仅适用与使用DispatcherServlet的web应用,该事件发布之后,标志着一次http请求完成。
ServletRequestHandledEventRequestHandledEvent的子类,用于添加特定于 Servlet 的上下文信息。

package org.springframework.context;

import java.util.EventListener;

/**
 * Interface to be implemented by application event listeners.
 *
 * <p>Based on the standard {@code java.util.EventListener} interface
 * for the Observer design pattern.
 *
 * <p>As of Spring 3.0, an {@code ApplicationListener} can generically declare
 * the event type that it is interested in. When registered with a Spring
 * {@code ApplicationContext}, events will be filtered accordingly, with the
 * listener getting invoked for matching event objects only.
 *
 * @author Rod Johnson
 * @author Juergen Hoeller
 * @param <E> the specific {@code ApplicationEvent} subclass to listen to
 * @see org.springframework.context.ApplicationEvent
 * @see org.springframework.context.event.ApplicationEventMulticaster
 * @see org.springframework.context.event.EventListener
 */
@FunctionalInterface
public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {

	/**
	 * Handle an application event.
	 * @param event the event to respond to
	 */
	void onApplicationEvent(E event);

}

三、 SmartApplicationListener,GenericApplicationListener

ApplicationListener监听器
通过ApplicationListener监听多个事件
SpringBoot-事件监听的4种实现方式
SpringBoot中ApplicationListener事件监听的6种方式
Spring源码系列(十)——ApplicationEvent事件机制源码分析

SpringBoot-5.ApplicationListener子接口GenericApplicationListener, SmartApplicationListener
09_springboot整合事件监听机制 - SmartApplicationListener

总结

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值