java - 哪个是 CDI @Produces 注释的 Spring 等效项?

标签 java spring dependency-injection cdi

当我使用 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/

相关文章:

重命名包后未加载 symfony2 包扩展

java - 在创建初始 JAX-WS 项目方面需要帮助

java - 如何使用 Jena 查找本体的语句数量

java - 如何使用自己的数据库用户使用 spring security 和 JWT 进行身份验证

spring - JSP属性文件读取

java - 有没有办法在没有 Spring 安全的情况下使用 Spring 过滤器链?

dependency-injection - 如何使用 Unity 在每次处理旧实例时解析新实例

c# - 如何使用 Autofac 注册开放通用接口(interface)的所有实现

java - 我应该如何在 Runnable 中设置我的 Future 结果?

java - 关于java中ADT树的问题