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?
Order of bean initialization should not matter since the fields are injected after creating the bean. The only problem where the order will matter is when the bean is needed at constructor argument of other class, but Spring notices this and will solve it.
Bean life cycle is managed by the spring container. When we run the program then, first of all, the spring container gets started. After that, the container creates the instance of a bean as per the request, and then dependencies are injected. And finally, the bean is destroyed when the spring container is closed.
2.1.springframework. beans. factory. InitializingBean interface allows a bean to perform initialization work after all necessary properties on the bean have been set by the container.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With