Java Spring Bean 初始化顺序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3187899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:13:57  来源:igfitidea点击:

Order of Spring Bean Initialization

javaspring

提问by Sandeep Jindal

I have multiple context files. Requirement is: one particular Bean (which makes some configuration changes) to be initialized first among rest of beans.

我有多个上下文文件。要求是:在其余 bean 中首先初始化一个特定的 Bean(进行一些配置更改)。

Is there a way to get this bean loaded first?

有没有办法先加载这个bean?

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.

一种选择是使用depends-on 属性。但这需要更新所有其余的 bean,因此似乎不是最佳解决方案。

Do we have better options?

我们有更好的选择吗?

采纳答案by dira

IMHO you should wait until they fix https://jira.spring.io/browse/SPR-3948

恕我直言,你应该等到他们修复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).

一种可能的方法是使用依赖属性。但同样,我不想在所有其余 bean 中添加此属性(这可能是我的最后手段)。

Actually, you don't need to use depends-on on EACH AND EVERY BEAN in each and every applicationContext.xml.

实际上,您不需要在每个 applicationContext.xml 中使用依赖于 EACH AND EVERY BEAN。

Use <import />in all "lower-lvel" applicationContext.xml to import the topmost applicationContext.xml.

使用<import />在所有的“低lvel” applicationContext.xml中导入最上面的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.

并且只在最顶层的 applicationContext.xml 中使用每个 bean 定义中的依赖属性,除非<bean />你想首先加载。

回答by Carl Smotricz

I'm not a Spring expert and likely to be shouted down by someone who is. But until then...

我不是 Spring 专家,很可能会被那些是的人大喊大叫。但在那之前...

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.

在存在歧义的地方,我猜想 Spring 会按照它在配置文件中遇到的顺序加载/应用东西。因此,作为第一个也是最简单的近似,我会尽量确保您要首先初始化的内容是配置文件中的第一件事。

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.

如果它都是分层的,那么您将希望您的“第一个”配置要么在调用其他文件之前位于“主”文件中,要么在可能的情况下位于第一个调用的文件中。

回答by blacelle

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)

In 要求对 bean 进行排序是有意义的,通常用于在业务 bean 之前加载技术 bean(例如添加 MBean)(就像在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.

使用 BeanPostProcessor 是另一种在 bean 中提供一些排序的方法。检查 AbstractApplicationContext.refresh() 以了解它是如何执行的。

回答by Wim Deblauwe

I managed to influence the bean startup order by annotating my bean with @Order: See http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/core/annotation/Order.html

我设法通过注释我的 bean 来影响 bean 启动顺序@Order:见http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/core/annotation/Order.html