加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 如何确保首先调用Spring @EventListener?

发布时间:2020-12-15 04:32:35 所属栏目:Java 来源:网络整理
导读:实现ApplicationListener接口时,有一个选项可以实现Ordered来指定调用顺序. 现在在 Spring 4.2中有一个使用@EventListener注释的选项.有没有办法承诺我的事件监听器将首先被调用? 解决方法 在@EventListener方法上使用 @Order 批注: @EventListener(MyEven
实现ApplicationListener接口时,有一个选项可以实现Ordered来指定调用顺序.
现在在 Spring 4.2中有一个使用@EventListener注释的选项.有没有办法承诺我的事件监听器将首先被调用?

解决方法

在@EventListener方法上使用 @Order批注:

@EventListener(MyEvent.class)
@Order(10)
public void myListener() { /* ... */ }

与Ordered接口一样,较低的值具有较高的优先级.

@EventListener JavaDoc开始:

It is also possible to define the order in which listeners for a certain event are invoked. To do so,add Spring’s common @Order annotation alongside this annotation.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读