Spring @Order in collections

It took me a while to understand why is the order not correct in the following code:

class A {
  @Autowired Collection<B> beans;
interface B {}
@Component @Order(1) class C implements B {}
@Component @Order(2) class D implements B {}

You’re welcome to guess why these are injected in the wrong order. No idea? What if I change it to List?

class A {
  @Autowired List<B> beans;

In this case ordered injection works properly. In general it makes some sense since Collection does not make any guarantees about the order. And I clearly understand framework designers here. Still this behavior is not clear from the first point of view.

This entry was posted in Development, Uncategorized and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s