1

Suppose I'm injecting list of some beans:

@Autowired
List<SomeBean> beans;

What is default injection order in this situation?

I know about Ordered interface and @Order annotation, I'm asking only about default behavior.

What I've noticed is that in case of manual bean registration:

context.register(SomeBeanA.class);
context.register(SomeBeanB.class);
context.register(SomeBeanC.class);

This beans is injected in the exact same order as I registered them: 1 element in list is SomeBeanA, 2 — SomeBeanB, 3 — SomeBeanC.

Is there is any guarantee of this behavior? I mean can I be sure that it won't change in further release?

Thanks.

Dmytro
  • 1,850
  • 3
  • 14
  • 20

1 Answers1

2

If you want to guarantee the order of the autowired list I would use the order interface or annotation.

In fact the list is ordered anyway and each bean not declaring an explicit order resolves to a default order. (Which is min integer by default if I'm not mistaken)

Take a look at this class: https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/core/annotation/AnnotationAwareOrderComparator.html

Martin Frey
  • 10,025
  • 4
  • 25
  • 30
  • Yeah, but I'm wondering about default behavior. So if by default all beans have the same order(Integer.MAX not min BTW), then how it sorts them? – Dmytro Dec 02 '15 at 18:35