Gili
Gili

Reputation: 213

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

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?

Upvotes: 18

Views: 16605

Answers (1)

Adam Michalik
Adam Michalik

Reputation: 9955

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.

Upvotes: 30

Related Questions