当我使用 CDI 时,我可以使用 @Produces
注解创建一个生产者方法被调用来选择实现接口(interface)的bean将由@Inject
注解注入(inject)。
现在我正在使用 Spring,但我没有找到任何类似的东西。当我使用 @Autowired
注释时,我需要使用什么来实现与 CDI 中的 @Produces
注释相同的结果?
最佳答案
您正在寻找 @Bean
:
@Bean is a method-level annotation and a direct analog of the XML
<bean/>
element. The annotation supports most of the attributes offered by<bean/>
, such as: init-method, destroy-method, autowiring, lazy-init, dependency-check, depends-on and scope.
示例(取自上面的链接):
@Configuration
public class AppConfig {
//similar to @Produces CDI annotation
@Bean
public TransferService transferService() {
return new TransferServiceImpl();
}
}
我建议您阅读以下内容:Spring DI and CDI comparative study
关于java - 哪个是 CDI @Produces 注释的 Spring 等效项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25876402/