|
| 1 | +## SpringBoot监听器实现 |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +### SpringBoot 系统监听器介绍 |
| 6 | + |
| 7 | +```java |
| 8 | +/** |
| 9 | + * Interface to be implemented by application event listeners. |
| 10 | + * Based on the standard {@code java.util.EventListener} interface |
| 11 | + * for the Observer design pattern. |
| 12 | + * |
| 13 | + * <p>As of Spring 3.0, an ApplicationListener can generically declare the event type |
| 14 | + * that it is interested in. When registered with a Spring ApplicationContext, events |
| 15 | + * will be filtered accordingly, with the listener getting invoked for matching event |
| 16 | + * objects only. |
| 17 | + * |
| 18 | + * @author Rod Johnson |
| 19 | + * @author Juergen Hoeller |
| 20 | + * @param <E> the specific ApplicationEvent subclass to listen to |
| 21 | + * @see org.springframework.context.event.ApplicationEventMulticaster |
| 22 | + */ |
| 23 | +@FunctionalInterface |
| 24 | +public interface ApplicationListener<E extends ApplicationEvent> extends EventListener { |
| 25 | + |
| 26 | + /** |
| 27 | + * Handle an application event. |
| 28 | + * @param event the event to respond to |
| 29 | + */ |
| 30 | + void onApplicationEvent(E event); |
| 31 | +} |
| 32 | +``` |
| 33 | + |
| 34 | +- SpringBoot中可以通过实现ApplicationListener接口类,来完成系统事件的监听 |
| 35 | +- 这个监听器是机遇java.util.EventListener标准来实现的,是一个ObServer观察者模式 |
| 36 | +- 从Spring3.0开始,ApplicationListener可以通过声明感兴趣的事件进行触发 |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | +### SpringBoot 系统广播器介绍 |
| 41 | + |
| 42 | + ```java |
| 43 | + /** |
| 44 | + * Interface to be implemented by objects that can manage a number of |
| 45 | + * {@link ApplicationListener} objects, and publish events to them. |
| 46 | + * |
| 47 | + * <p>An {@link org.springframework.context.ApplicationEventPublisher}, typically |
| 48 | + * a Spring {@link org.springframework.context.ApplicationContext}, can use an |
| 49 | + * ApplicationEventMulticaster as a delegate for actually publishing events. |
| 50 | + * |
| 51 | + * @author Rod Johnson |
| 52 | + * @author Juergen Hoeller |
| 53 | + * @author Stephane Nicoll |
| 54 | + */ |
| 55 | + public interface ApplicationEventMulticaster { |
| 56 | + ``` |
| 57 | + |
| 58 | +- 用来管理ApplicationListener事件监听器,比如事件监听器的添加、删除等 |
| 59 | +- 用来遍历所有的ApplicationListener事件监听器,触发ApplicationEvent事件 |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | +#### (1)Spring 系统广播器的具体方法介绍 |
| 64 | + |
| 65 | +```java |
| 66 | +public interface ApplicationEventMulticaster { |
| 67 | + |
| 68 | + // 添加一个 ApplicationListener 事件监听器 |
| 69 | + void addApplicationListener(ApplicationListener<?> listener); |
| 70 | + |
| 71 | + // 删除一个 ApplicationListener 事件监听器 |
| 72 | + void removeApplicationListener(ApplicationListener<?> listener); |
| 73 | + |
| 74 | + // 删除所有事件监听器 |
| 75 | + void removeAllListeners(); |
| 76 | + |
| 77 | + // 广播 ApplicationEvent 事件 |
| 78 | + void multicastEvent(ApplicationEvent event); |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | +### Spring 系统事件介绍 |
| 85 | + |
| 86 | +<img src="https://tva1.sinaimg.cn/large/008i3skNly1gt3puhrolmj323g0u041t.jpg" alt="image-20210803171306277" align="left" width="1000" /> |
| 87 | + |
| 88 | +- EventObject:具体的事件描述 |
| 89 | +- ApplicationEvent:应用事件描述 |
| 90 | +- SpringApplicationEvent:框架应用事件描述 |
| 91 | +- ApplicationContextInitializedEvent:框架应用上下文初始化阶段的事件 |
| 92 | +- ApplicationEnvironmentPreparedEvent:框架应用环境属性准备好阶段的事件 |
| 93 | +- ApplicationFeiledEvent:框架启动失败时的事件 |
| 94 | +- ApplicationPreparedEvent:框架准备阶段的事件 |
| 95 | +- ApplicationReadyEvent:框架准备好时的事件 |
| 96 | +- ApplicationStartedEvent:框架启动完成时的事件 |
| 97 | +- ApplicationStartingEvent:框架启动中的事件 |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | +### Spring 事件发送的顺序 |
| 102 | + |
| 103 | +<img src="https://tva1.sinaimg.cn/large/008i3skNly1gt3q2eu1b0j322x0u0q5p.jpg" alt="image-20210803172047686" align="left" width="1000" /> |
| 104 | + |
| 105 | +- 框架准备启动 |
| 106 | +- 触发ApplicationStaringEvent事件,告知框架已开始启动 |
| 107 | +- 触发ApplicationEnvironmentPreparedEvent事件,告知框架环境属性、我们指定的一些属性已经准备完成 |
| 108 | +- 触发ApplicationContextInitializedEvent事件,告知框架ApplicationContextInitializer的系统上下文已经初始化好 |
| 109 | +- Bean的准备、启动、准备好阶段,会分别触发ApplicationPreparedEvent、ApplicationStartedEvent、ApplicationReadyEvent事件; |
| 110 | +- 整个SpringBoot启动失败时,会触发ApplicationFailedEvent事件; |
| 111 | +- 框架启动完毕 |
| 112 | + |
| 113 | + |
| 114 | + |
0 commit comments