tags:

views:

162

answers:

3

Hi,

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

A: 

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
A: 

I don't know of any mechanism to do this, although one option is to implement the BeanFactoryPostProcessor interface. This has a postProcessBeanFactory() method which will be called after all bean definitions have been loaded, but before any beans have been instantiated or initialized. So you could potentially do your logic inside this method.

skaffman
+1  A: 

IMHO you should wait until they fix http://jira.springframework.org/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.

becomputer06
did you mean the depends-on attribute for import tag?
Sandeep Jindal
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