Spring Bean初始化的顺序 [英] Order of Spring Bean Initialization
问题描述
我有多个上下文文件。要求是:一个特定的Bean(进行一些配置更改)首先在其余bean中初始化。
I have multiple context files. Requirement is: one particular Bean (which makes some configuration changes) to be initialized first among rest of beans.
有没有办法先装入这个bean?
Is there a way to get this bean loaded first?
一个选项是使用depends-on`属性。但这需要更新所有剩余的bean,因此似乎不是最佳解决方案。
我们有更好的选择吗?
推荐答案
恕我直言,你应该等到他们修复 https://jira.spring.io/browse/SPR-3948
IMHO you should wait until they fix https://jira.spring.io/browse/SPR-3948
一种可能的方法是使用依赖属性。但同样,我不想在所有剩余的bean中添加此属性(这可能是我的最后手段)。
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).
实际上,您不需要在每个applicationContext.xml中对每个和每个BEAN使用依赖。
Actually, you don't need to use depends-on on EACH AND EVERY BEAN in each and every applicationContext.xml.
使用 < import />
在所有lower-lvelapplicationContext.xml中导入最顶层的applicationContext.xml。
Use <import />
in all "lower-lvel" applicationContext.xml to import the topmost applicationContext.xml.
并使用取决于 - on each和ever bean定义中的属性仅在最顶层的applicationContext.xml中,除了你想首先加载的< bean />
。
And use depends-on attribute in each and ever bean definition only in topmost applicationContext.xml, except the <bean />
that you wanna load first.
这篇关于Spring Bean初始化的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!