38

I have multiple context files. Requirement is: one particular Bean (which makes some configuration changes) to be initialized first among rest of beans.

Is there a way to get this bean loaded first?

One option is using depends-on` attribute. But that would require updating all the rest of the beans, thus seems not to be best solution.

Do we have better options?

Sandeep Jindal
  • 14,510
  • 18
  • 83
  • 121

4 Answers4

14

IMHO you should wait until they fix https://jira.spring.io/browse/SPR-3948

One probable way is to use depends-on attribute. But again, I don't want to add this attribute in all the rest of the beans (this is probably the last resort for me).

Actually, you don't need to use depends-on on EACH AND EVERY BEAN in each and every applicationContext.xml.

Use <import /> in all "lower-lvel" applicationContext.xml to import the topmost applicationContext.xml.

And use depends-on attribute in each and ever bean definition only in topmost applicationContext.xml, except the <bean /> that you wanna load first.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
dira
  • 30,304
  • 14
  • 54
  • 69
  • did you mean the depends-on attribute for import tag? – Sandeep Jindal Jul 09 '10 at 02:40
  • I mean use in lower-level appContext.xml to import top-level appContext.xml. And use depends-on attribute for only in top-level appContext.xml – dira Jul 09 '10 at 05:18
  • init-order wouldn't be as bad as depends-on. Can someone verify if negative numbers are acceptable for init-order. And if the default is 1 or 0? – Ustaman Sangat Mar 06 '12 at 16:13
2

I managed to influence the bean startup order by annotating my bean with @Order: See http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/core/annotation/Order.html

Wim Deblauwe
  • 25,113
  • 20
  • 133
  • 211
1

In can be meaningful to require ordering of beans, typically for technical beans (e.g. adding MBeans) to be loaded before business beans (just like it is proposed in http://jira.springframework.org/browse/SPR-3948)

Using BeanPostProcessor is another way to provide some ordering in your beans. Check AbstractApplicationContext.refresh() to see how it is enforced.

blacelle
  • 2,199
  • 1
  • 19
  • 28
-4

I'm not a Spring expert and likely to be shouted down by someone who is. But until then...

Where there's room for ambiguity, I would guess that Spring loads/applies stuff in the order it encounters it in the configuration files. Thus, as a first and simplest approximation, I would try to ensure that the thing you want initialized first is one of the first things in your configuration files.

If it's all hierarchical, then you'll want your "first" configurations to either be in the "main" file before the others are invoked or if possible in the first invoked file.

Carl Smotricz
  • 66,391
  • 18
  • 125
  • 167
  • 5
    Spring loads certain types of beans (like BeanPostProcessor) earlier than others and it also resolves dependency trees. I think it is no use to try to "order" declarations in some way - it will just not work at all or break immediately after any change to your beans. – Sergey Makarov Jul 10 '13 at 12:19