Spring 的 ApplicationEvent
介绍
Spring 的事件(Application Event)为 Bean 与 Bean 之间的消息通信提供了支持。当一个 Bean 处理完一个任务之后,希望另一个 Bean 知道并能做相应的处理,这时我们就需要让另一个 Bean 监听当前 Bean 所发送的时间。
使用
Spring 的事件需要遵循以下流程
- 自定义事件集成 ApplicationEvent
- 定义事件监听器,实现 ApplicationListener
- 使用容器发布事件
原理
(观察者模式)
问题
SpringMVC 中使用 ApplicationEvent 会出现二次调用的问题
-
解决方案
- 使用
root context
发布事件
- 使用