Can I ensure that one of my Spring ApplicationListeners gets executed last?
dependency-injection, event-handling, java, spring
Solution
All your beans implementing `ApplicationListener` should also implement `Ordered` and provide reasonable order value. The lower the value, the sooner your listener will be invoked:
class FirstListener implements ApplicationListener<Foo>, Ordered {
public int getOrder() {
return 10;
}
//...
}
class SecondListener implements ApplicationListener<Foo>, Ordered {
public int getOrder() {
return 20;
}
//...
}
class LastListener implements ApplicationListener<Foo>, Ordered {
public int getOrder() {
return LOWEST_PRECEDENCE;
}
//...
}
Moreover you can implement `PriorityOrdered` to make sure one of your listeners is always invoked first.
Problem
I have several services that are listening for Spring events to make changes to my underlying data model. These all work by implementing `ApplicationListener<Foo>`. Once all of the `Foo` listeners modify the underlying data model, my user interface needs to refresh to reflect the changes (think `fireTableDataChanged()`). Is there any way to ensure that a specific listener for `Foo` is always last? Or is there any way to call a function when all other listeners are done? I'm using annotation based wiring and Java config, if that matters.