From 626a4534688681567bb318e204141d6306289497 Mon Sep 17 00:00:00 2001 From: ipipman Date: Thu, 5 Aug 2021 18:59:31 +0800 Subject: [PATCH] add spring listens design pattern markdown in spring boot code analysis --- ...17344円270円216円345円256円236円347円216円260円.md" | 114 ++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 "springboot-source-code-analysis/(6)346円241円206円346円236円266円345円206円205円347円233円221円345円220円254円345円231円250円350円256円276円350円256円241円346円250円241円345円274円217円344円270円216円345円256円236円347円216円260円.md" diff --git "a/springboot-source-code-analysis/(6)346円241円206円346円236円266円345円206円205円347円233円221円345円220円254円345円231円250円350円256円276円350円256円241円346円250円241円345円274円217円344円270円216円345円256円236円347円216円260円.md" "b/springboot-source-code-analysis/(6)346円241円206円346円236円266円345円206円205円347円233円221円345円220円254円345円231円250円350円256円276円350円256円241円346円250円241円345円274円217円344円270円216円345円256円236円347円216円260円.md" new file mode 100644 index 0000000..5ec2785 --- /dev/null +++ "b/springboot-source-code-analysis/(6)346円241円206円346円236円266円345円206円205円347円233円221円345円220円254円345円231円250円350円256円276円350円256円241円346円250円241円345円274円217円344円270円216円345円256円236円347円216円260円.md" @@ -0,0 +1,114 @@ +## SpringBoot监听器实现 + + + +### SpringBoot 系统监听器介绍 + +```java +/** + * Interface to be implemented by application event listeners. + * Based on the standard {@code java.util.EventListener} interface + * for the Observer design pattern. + * + *

As of Spring 3.0, an ApplicationListener can generically declare the event type + * that it is interested in. When registered with a Spring ApplicationContext, events + * will be filtered accordingly, with the listener getting invoked for matching event + * objects only. + * + * @author Rod Johnson + * @author Juergen Hoeller + * @param the specific ApplicationEvent subclass to listen to + * @see org.springframework.context.event.ApplicationEventMulticaster + */ +@FunctionalInterface +public interface ApplicationListener extends EventListener { + + /** + * Handle an application event. + * @param event the event to respond to + */ + void onApplicationEvent(E event); +} +``` + +- SpringBoot中可以通过实现ApplicationListener接口类,来完成系统事件的监听 +- 这个监听器是机遇java.util.EventListener标准来实现的,是一个ObServer观察者模式 +- 从Spring3.0开始,ApplicationListener可以通过声明感兴趣的事件进行触发 + + + +### SpringBoot 系统广播器介绍 + + ```java + /** + * Interface to be implemented by objects that can manage a number of + * {@link ApplicationListener} objects, and publish events to them. + * + *

An {@link org.springframework.context.ApplicationEventPublisher}, typically + * a Spring {@link org.springframework.context.ApplicationContext}, can use an + * ApplicationEventMulticaster as a delegate for actually publishing events. + * + * @author Rod Johnson + * @author Juergen Hoeller + * @author Stephane Nicoll + */ + public interface ApplicationEventMulticaster { + ``` + +- 用来管理ApplicationListener事件监听器,比如事件监听器的添加、删除等 +- 用来遍历所有的ApplicationListener事件监听器,触发ApplicationEvent事件 + + + +#### (1)Spring 系统广播器的具体方法介绍 + +```java +public interface ApplicationEventMulticaster { + + // 添加一个 ApplicationListener 事件监听器 + void addApplicationListener(ApplicationListener listener); + + // 删除一个 ApplicationListener 事件监听器 + void removeApplicationListener(ApplicationListener listener); + + // 删除所有事件监听器 + void removeAllListeners(); + + // 广播 ApplicationEvent 事件 + void multicastEvent(ApplicationEvent event); +} +``` + + + +### Spring 系统事件介绍 + +image-20210803171306277 + +- EventObject:具体的事件描述 +- ApplicationEvent:应用事件描述 +- SpringApplicationEvent:框架应用事件描述 +- ApplicationContextInitializedEvent:框架应用上下文初始化阶段的事件 +- ApplicationEnvironmentPreparedEvent:框架应用环境属性准备好阶段的事件 +- ApplicationFeiledEvent:框架启动失败时的事件 +- ApplicationPreparedEvent:框架准备阶段的事件 +- ApplicationReadyEvent:框架准备好时的事件 +- ApplicationStartedEvent:框架启动完成时的事件 +- ApplicationStartingEvent:框架启动中的事件 + + + +### Spring 事件发送的顺序 + +image-20210803172047686 + +- 框架准备启动 +- 触发ApplicationStaringEvent事件,告知框架已开始启动 +- 触发ApplicationEnvironmentPreparedEvent事件,告知框架环境属性、我们指定的一些属性已经准备完成 +- 触发ApplicationContextInitializedEvent事件,告知框架ApplicationContextInitializer的系统上下文已经初始化好 +- Bean的准备、启动、准备好阶段,会分别触发ApplicationPreparedEvent、ApplicationStartedEvent、ApplicationReadyEvent事件; +- 整个SpringBoot启动失败时,会触发ApplicationFailedEvent事件; +- 框架启动完毕 + + +

AltStyle によって変換されたページ (->オリジナル) /