bra*_*orm 6 spring dependency-injection spring-mvc spring-annotations
我在 spring 应用程序中有三个配置文件。
@Configuration
public class FooConfig { ... }
@Configuration
public class BarConfig { ... }
@Configuration
public class FooBarConfig { ... }
Run Code Online (Sandbox Code Playgroud)
加载 bean 的顺序是什么?我可以bean在FooConfigin 中使用定义的,BarConfig反之亦然吗?
编辑
这可以正常工作。但我怀疑它是否因为偶然而有效。这里有一个歧义,因为使用了不同的配置文件,并且解析它们的顺序对于正确加载 bean 很重要。
请看一下spring文档
您可以使用依赖注入 @Autowired来引用在其他 java 配置类上声明的 bean,但是确定自动装配 bean 定义的确切位置以及解决方案是使用仍然可能不明确 @Import
@Configuration
@Import({FooConfig.class, FooBarConfig .class})
public class FooBarConfig {
//use Autowire to import bean declared in both FooConfig and FooBarConfig
}
Run Code Online (Sandbox Code Playgroud)
编辑:
至于如果 bean A 依赖于 bean B 的顺序,您可以保证 B 将在 A 之前实例化,如果没有依赖注入来维护该顺序,一个技巧或解决方法是使用@Resource.
| 归档时间: |
|
| 查看次数: |
9838 次 |
| 最近记录: |