Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I ensure that a Spring @EventListener will be invoked first?

Tags:

java

spring

When implementing the ApplicationListener interface there was an option to implement Ordered to specify the order of invocation. Now in Spring 4.2 there is an option to use the @EventListener annotation. Is there a way to promise that my event listener will be called first?

like image 359
Gili Avatar asked Feb 24 '16 14:02

Gili


1 Answers

Use the @Order annotation on the @EventListener method:

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

Just as with the Ordered interface, lower values have higher priority.

From the @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.

like image 63
Adam Michalik Avatar answered Sep 28 '22 08:09

Adam Michalik