'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?



Solution 1:[1]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1