Welcome

Sorting Your Beans With Spring OrderComparator

Sometimes you may need to execute your collection of beans in a a specified order. For example, in one of our projects, we have collection of EventHandlers which operate on when certain Events occur. For each event there might be more than one EventHandler instance that need to operate on. Most of the time it is not important if one executes before the other or vice versa. However, from time to time there happens that some EventHandler instances should wait for successful completion of some other EventHandler instances. Rest could still operate on unspecified order.

At this point, we just want those instances get ordered among themselves and ignore others. Thanks to Spring as it provides simple but nice OrderComparator implementation exactly for this purpose. It simply is used to sort instances implementing Spring’s Ordered interface, putting those other instances not implementing that interface at the end of collection, in arbitrary order.

We manage EventHandler instances as Spring beans, and inject them into another bean as array. That bean implements Spring’s InitializingBean interface as well, so that after EventHandler collection property is initialized properly, it could be sorted out using OrderComparator. For this purpose Arrays.sort() or Collections.sort() static methods could be used both having collection or objects, and Comparator instance as input parameters.Sometimes you may need to execute your collection of beans in a a specified order. For example, in one of our projects, we have collection of EventHandlers which operate on when certain Events occur. For each event there might be more than one EventHandler instance that need to operate on. Most of the time it is not important if one executes before the other or vice versa. However, from time to time there happens that some EventHandler instances should wait for successful completion of some other EventHandler instances. Rest could still operate on unspecified order.

At this point, we just want those instances get ordered among themselves and ignore others. Thanks to Spring as it provides simple but nice OrderComparator implementation exactly for this purpose. It simply is used to sort instances implementing Spring’s Ordered interface, putting those other instances not implementing that interface at the end of collection, in arbitrary order.

We manage EventHandler instances as Spring beans, and inject them into another bean as array. That bean implements Spring’s InitializingBean interface as well, so that after EventHandler collection property is initialized properly, it could be sorted out using OrderComparator. For this purpose Arrays.sort() or Collections.sort() static methods could be used both having collection or objects, and Comparator instance as input parameters.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.