forked from ipipman/JavaSpringBootSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
[pull] master from ipipman:master #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
114 changes: 114 additions & 0 deletions
springboot-source-code-analysis/(6)框架内监听器设计模式与实现.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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. | ||
| * | ||
| * <p>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 <E> the specific ApplicationEvent subclass to listen to | ||
| * @see org.springframework.context.event.ApplicationEventMulticaster | ||
| */ | ||
| @FunctionalInterface | ||
| public interface ApplicationListener<E extends ApplicationEvent> 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. | ||
| * | ||
| * <p>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 系统事件介绍 | ||
|
|
||
| <img src="https://tva1.sinaimg.cn/large/008i3skNly1gt3puhrolmj323g0u041t.jpg" alt="image-20210803171306277" align="left" width="1000" /> | ||
|
|
||
| - EventObject:具体的事件描述 | ||
| - ApplicationEvent:应用事件描述 | ||
| - SpringApplicationEvent:框架应用事件描述 | ||
| - ApplicationContextInitializedEvent:框架应用上下文初始化阶段的事件 | ||
| - ApplicationEnvironmentPreparedEvent:框架应用环境属性准备好阶段的事件 | ||
| - ApplicationFeiledEvent:框架启动失败时的事件 | ||
| - ApplicationPreparedEvent:框架准备阶段的事件 | ||
| - ApplicationReadyEvent:框架准备好时的事件 | ||
| - ApplicationStartedEvent:框架启动完成时的事件 | ||
| - ApplicationStartingEvent:框架启动中的事件 | ||
|
|
||
|
|
||
|
|
||
| ### Spring 事件发送的顺序 | ||
|
|
||
| <img src="https://tva1.sinaimg.cn/large/008i3skNly1gt3q2eu1b0j322x0u0q5p.jpg" alt="image-20210803172047686" align="left" width="1000" /> | ||
|
|
||
| - 框架准备启动 | ||
| - 触发ApplicationStaringEvent事件,告知框架已开始启动 | ||
| - 触发ApplicationEnvironmentPreparedEvent事件,告知框架环境属性、我们指定的一些属性已经准备完成 | ||
| - 触发ApplicationContextInitializedEvent事件,告知框架ApplicationContextInitializer的系统上下文已经初始化好 | ||
| - Bean的准备、启动、准备好阶段,会分别触发ApplicationPreparedEvent、ApplicationStartedEvent、ApplicationReadyEvent事件; | ||
| - 整个SpringBoot启动失败时,会触发ApplicationFailedEvent事件; | ||
| - 框架启动完毕 | ||
|
|
||
|
|
||
|
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.