在 Spring 的 Java 配置中自动装配 bean
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28747743/
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
Autowire a bean within Spring's Java configuration
提问by Avanst
Is it possible to use Spring's @Autowired
annotation within a Spring configuration written in Java?
是否可以在用@Autowired
Java 编写的 Spring 配置中使用 Spring 的注释?
For example:
例如:
@Configuration
public class SpringConfiguration{
@Autowired
DataSource datasource;
@Bean
public DataSource dataSource(){
return new dataSource();
}
// ...
}
Obviously a DataSource interface cannot be directly instantiated but I directly instantiated it here for simplification. Currently, when I try the above, the datasource object remains null and is not autowired by Spring.
很明显,一个DataSource接口不能直接实例化,为了简化,我这里直接实例化了。目前,当我尝试上述操作时,数据源对象保持为 null,并且不会由 Spring 自动装配。
I got @Autowired
to work successfully with a Hibernate SessionFactory
object by returning a FactoryBean<SessionFactory>
.
我@Autowired
用一个Hibernate成功运行SessionFactory
对象通过返回FactoryBean<SessionFactory>
。
So my question specifically: is there a way to do that with respect to a DataSource
? Or more generally, what is the method to autowire a bean within a Spring Java Configuration?
所以我的问题是:有没有办法对 a 做到这一点DataSource
?或者更一般地说,在 Spring Java 配置中自动装配 bean 的方法是什么?
I should note I am using Spring version 3.2.
我应该注意到我使用的是 Spring 3.2 版。
采纳答案by Sotirios Delimanolis
If you need a reference to the DataSource
bean within the same @Configuration
file, just invoke the bean method.
如果您需要DataSource
在同一@Configuration
文件中引用 bean,只需调用 bean 方法。
@Bean
public OtherBean someOtherBean() {
return new OtherBean(dataSource());
}
or have it autowired into the @Bean
method
或者让它自动连接到@Bean
方法中
@Bean
public OtherBean someOtherBean(DataSource dataSource) {
return new OtherBean(dataSource);
}
The lifecycle of a @Configuration
class sometimes prevents autowiring like you are suggesting.
@Configuration
类的生命周期有时会像您建议的那样阻止自动装配。
回答by javahuang
maybe you can use constructor to inject some bean from other configuration file to your configuration,while @Autowired may not work correct in the configuration file
也许您可以使用构造函数将其他配置文件中的一些 bean 注入您的配置,而 @Autowired 在配置文件中可能无法正常工作
@Configuration
public class AppConfig {
private DataSource dataSource;
public AppConfig(DataSource dataSource) {
this.dataSource = dataSource;
}
@Bean
public OtherBean otherBean(){
return new OtherBean(dataSource);
}
}
回答by Shruthi H R
beans configured with @Configuration class can't be autowired ,
用@Configuration 类配置的 bean 不能自动装配,
So you cannot use @Autowiring and @configuration together
所以你不能同时使用@Autowiring 和@configuration
回答by tkruse
For completeness sake: Yes it is technically possible to use @Autowired annotation in spring @Configuration classes, and it works the same way as for other spring beans. But the accepted answer shows how the original problem should be solved.
为了完整起见:是的,技术上可以在 spring @Configuration 类中使用 @Autowired 注释,它的工作方式与其他 spring bean 相同。但是接受的答案显示了应该如何解决原始问题。