Search code examples
javaspring

Order of Spring Bean Initialization


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?


Solution

  • 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.