使用CDI生产者会导致模棱两可的依赖关系异常 [英] Using a CDI producer causes ambiguous dependencies exception

查看:116
本文介绍了使用CDI生产者会导致模棱两可的依赖关系异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将 ApplicationScoped bean的相同实例注入到应用程序的多个位置,并创建了以下使用 @PostConstruct 注释可初始化bean,而 @Produces 注释可返回该bean的相同实例。

I have a requirement to Inject the same instance of an ApplicationScoped bean into several places of my application and have created the following factory class which uses the @PostConstruct annotation to initialize the bean and the @Produces annotation to return the same instance of the bean.

@ApplicationScoped
public class CommandBusFactory implements Serializable {

    private static final long serialVersionUID = 1L;

    private CommandBus commandBus;

    @PostConstruct
    public void init() {
        commandBus = new BasicCommandBus();
        // Do some stuff to configure the command bus
    }

    @Produces
    public CommandBus produceCommandBus() {
        return commandBus;
    }

}

我遇到的问题是部署应用程序时,GlassFish返回以下错误消息:

The problem I've got is when I deploy the application GlassFish returns the following error message:

Exception while loading the app : CDI deployment failure:WELD-001409 Ambiguous dependencies for type [CommandBus] with qualifiers [@Default] at injection point [[BackedAnnotatedField] @Inject private myapp.web.ToDoItemCommandController.commandBus]. Possible dependencies [[Producer Method [CommandBus] with qualifiers [@Any @Default] declared as [[BackedAnnotatedMethod] @Produces public myapp.core.cdi.CommandBusFactory.produceCommandBus()], Managed Bean [class myapp.core.commandhandling.BasicCommandBus] with qualifiers [@Any @Default]]]

我可以通过添加<$来克服此异常c $ c> @Alternative 注释到BasicCommandBus类,但这似乎并不是解决问题的最佳方法。

I can overcome this exception by adding the @Alternative annotation to the BasicCommandBus class, however this doesn't seem to be the best way of solving the problem.

我不想在将 CommandBus 注入到应用程序中的各处添加限定符,就像使用 CommandBus 的不同实现一样需要在多个位置更改代码。目的是工厂的更高版本将读取配置文件,并且根据配置文件中的值,它可能会创建不同类型的 CommandBus

I don't want to add a qualifier everywhere that I inject CommandBus into my application as using a different implementation of CommandBus would require changing the code in multiple places. The intention is that a later version of the factory will read a configuration file and depending upon a value in the configuration file it may create a different type of CommandBus.

我已阅读此问题的答案( https://stackoverflow.com/a/ 18782027/1274662 ),并理解为什么会抛出歧义依赖异常,但我不知道这是处理以下事实的最佳方法:鉴于我要决定,可以注入两个可能的bean

I have read the answer to this question (https://stackoverflow.com/a/18782027/1274662) and understand why the Ambiguous dependencies exception is being thrown but what I don't know is the best way to deal with the fact that there are two possible beans that could be injected given that I want to decide which implementation is used and how it is initialised in a central location.

我所遇到的问题是:


  1. BasicCommandBus 类上使用 @Alternative 批注

是否应该使用更好的方法来注入 ApplicationScoped 豆(即 Co mmandBus )放入我的应用程序的多个位置,同时控制创建的实现(即 BasicCommandBus EnhancedCommandBus )以及如何在中央位置对其进行初始化?

Is there a better approach that I should be using to Inject the same instance of an ApplicationScoped bean (i.e. CommandBus) into several places of my application whilst controlling which implementation created (i.e. BasicCommandBus or EnhancedCommandBus) and how it is initialised in a central location?


推荐答案

如果您希望仅生产者可以注入bean,则可以注释 BasicCommandBus EnhancedCommandBus @Vetoed 这样,您就不会在Bean与它之间产生歧义自我和生产者方法,并且在每个注入点将由生产者注入实例。

If you want your bean to be injectable only by the producer you can annotate BasicCommandBus and EnhancedCommandBus with @Vetoed this way you'll not have the ambiguity between the bean it self and the producer method and at every injection point it's the producer that will inject the instance.

这篇关于使用CDI生产者会导致模棱两可的依赖关系异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆