Take the 2-minute tour ×

In my Spring File I have multiple Beans (in multiple application context files being imported from one main application context).

My requirement is that, I want a particular Bean(which makes some configuration changes) to be initialized first among all the rest of beans.

Is there any where I can specify Spring to load that bean first? 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).

Can you please guide for the same?

Regards Sandeep Jindal

share|improve this question
add comment

3 Answers

up vote 12 down vote accepted

IMHO you should wait until they fix http://jira.springframework.org/browse/SPR-3948


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.

share|improve this answer
did you mean the depends-on attribute for import tag? –  Sandeep Jindal Jul 9 '10 at 2:40
I mean use <import /> in lower-level appContext.xml to import top-level appContext.xml. And use depends-on attribute for <bean /> only in top-level appContext.xml –  becomputer06 Jul 9 '10 at 5: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 6 '12 at 16:13
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
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
add comment

Your Answer


