哪个是Spring相当于CDI @Produces注释的? [英] Which is the Spring equivalent for the CDI @Produces annotation?
问题描述
当我使用CDI时,我可以使用 @Produces
注释,以创建一个要调用的生成器方法,以选择实现接口的bean将由 @Inject
注释。
When I was working with CDI, I could use the @Produces
annotation to create a producer method to be called to choose which bean that implemented an interface would be injected by the @Inject
annotation .
现在我正在使用Spring,但是我没有找到任何类似的东西。当我使用 @Autowired
@Produces 注释相同的结果>注释?
Now I am working with Spring, but I didn't find anything similar. What I need to use to achieve the same result I had with the @Produces
annotation in CDI when I use the @Autowired
annotation?
推荐答案
您正在寻找 @Bean
:
You're looking for @Bean
:
@Bean是XML元素的方法级注释和直接模拟。注释支持大多数提供的属性,例如:init-method,destroy-method,autowiring,lazy-init,dependency-check,depend-on和scope。
@Bean is a method-level annotation and a direct analog of the XML element. The annotation supports most of the attributes offered by , such as: init-method, destroy-method, autowiring, lazy-init, dependency-check, depends-on and scope.
示例(取自上面的链接):
Example (taken from link above):
@Configuration
public class AppConfig {
//similar to @Produces CDI annotation
@Bean
public TransferService transferService() {
return new TransferServiceImpl();
}
}
我建议您付款阅读: a href =http://mobiarch.wordpress.com/2013/01/11/spring-di-and-cdi-comparative-study/>春季DI和CDI比较研究
I suggest you to pay a read to this: Spring DI and CDI comparative study
这篇关于哪个是Spring相当于CDI @Produces注释的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!