Spring 的 ApplicationEvent

Spring 的 ApplicationEvent

介绍

Spring 的事件(Application Event)为 Bean 与 Bean 之间的消息通信提供了支持。当一个 Bean 处理完一个任务之后,希望另一个 Bean 知道并能做相应的处理,这时我们就需要让另一个 Bean 监听当前 Bean 所发送的时间。

使用

Spring 的事件需要遵循以下流程

  • 自定义事件集成 ApplicationEvent
  • 定义事件监听器,实现 ApplicationListener
  • 使用容器发布事件

原理

(观察者模式)

问题

SpringMVC 中使用 ApplicationEvent 会出现二次调用的问题

  • 解决方案

    • 使用 root context 发布事件
发布了161 篇原创文章 · 获赞 140 · 访问量 47万+

猜你喜欢

转载自blog.csdn.net/qq_37933685/article/details/102869852