Glassfish 4.1 CDI WELD-001409:含糊不清的依存关系 [英] Glassfish 4.1 CDI WELD-001409: Ambiguous dependencies

查看:105
本文介绍了Glassfish 4.1 CDI WELD-001409:含糊不清的依存关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在部署在 Glassfish 4.1 中的项目中。依赖关系注入是使用CDI进行的,在尝试部署应用程序时遇到问题。我遇到的错误如下(通常我在代码中翻译我的类的名称,所以整个帖子使用的是同一语言,我现在不再这样做,所以我可以复制/粘贴并避免一些错误)翻译打字错误):

I'm working in a project deployed in Glassfish 4.1. Dependency injection is made using CDI and I'm having an issue when trying to deploy the application. The error I'm getting is the following (usually I "translate" the name of my classes in the code so the whole post is in the same language, I`ll not do that now so I can copy/paste and avoid some translate typing mistakes):

org.glassfish.deployment.common.DeploymentException: CDI deployment failure:Exception List with 3 exceptions:
Exception 0 :
org.jboss.weld.exceptions.DeploymentException: WELD-001409: Ambiguous dependencies for type GestorUsuarios with qualifiers @Default
  at injection point [BackedAnnotatedField] @Inject private ar.edu.unt.sigea.servicios.impl.GestorPersonasImpl.gestorUsuarios
  at ar.edu.unt.sigea.servicios.impl.GestorPersonasImpl.gestorUsuarios(GestorPersonasImpl.java:0)
  Possible dependencies: 
  - Session bean [class ar.edu.unt.sigea.servicios.impl.GestorUsuariosImpl with qualifiers [@Any @Default]; local interfaces are [GestorUsuarios],
  - Managed Bean [class ar.edu.unt.sigea.servicios.impl.GestorUsuariosImpl] with qualifiers [@Any @Default]
        ...[Stack trace]...

Exception 0 :
org.jboss.weld.exceptions.DeploymentException: WELD-001409: Ambiguous dependencies for type RepositorioMenu with qualifiers @Default
  at injection point [BackedAnnotatedField] @Inject private ar.edu.unt.sigea.servicios.impl.GestorUsuariosImpl.repositorioMenu
  at ar.edu.unt.sigea.servicios.impl.GestorUsuariosImpl.repositorioMenu(GestorUsuariosImpl.java:0)
  Possible dependencies: 
  - Managed Bean [class ar.edu.unt.sigea.repositorio.RepositorioMenu] with qualifiers [@Any @Default],
  - Session bean [class ar.edu.unt.sigea.repositorio.RepositorioMenu with qualifiers [@Any @Default]; local interfaces are [RepositorioMenu]
        ...[Stack trace]...

Exception 0 :
org.jboss.weld.exceptions.DeploymentException: WELD-001409: Ambiguous dependencies for type RepositorioOperacion with qualifiers @Default
  at injection point [BackedAnnotatedField] @Inject ar.edu.unt.sigea.repositorio.RepositorioMenu.repositorioOperacion
  at ar.edu.unt.sigea.repositorio.RepositorioMenu.repositorioOperacion(RepositorioMenu.java:0)
  Possible dependencies: 
  - Managed Bean [class ar.edu.unt.sigea.repositorio.RepositorioOperacion] with qualifiers [@Any @Default],
  - Session bean [class ar.edu.unt.sigea.repositorio.RepositorioOperacion with qualifiers [@Any @Default]; local interfaces are [RepositorioOperacion]

(顺便说一句,我不知道为什么它说 3个例外,并且所有出现的编号都为例外0

(by the way, I don't know why it says 3 exceptions and all appear numbered as Exception 0)

@Stateless
public class GestorPersonasImpl implements GestorPersonas {

    @Inject
    private GestorUsuarios gestorUsuarios;

    public GestorUsuarios getGestorUsuarios() {return gestorUsuarios;}

    public void setGestorUsuarios(GestorUsuarios gestorUsuarios) {
        this.gestorUsuarios = gestorUsuarios;
    }
    // Other fields and methods
}

@Stateless
public class GestorUsuariosImpl implements GestorUsuarios {

    @Inject
    private RepositorioMenu repositorioMenu;

    public RepositorioMenu getRepositorioMenu() {return repositorioMenu;}

    public void setRepositorioMenu(RepositorioMenu repositorioMenu) {
        this.repositorioMenu = repositorioMenu;
    }
    // Other fields and methods
}

@Stateless
@LocalBean
public class RepositorioMenu extends RepositorioGenerico<Menu> {

    @Inject
    private RepositorioOperacion repositorioOperacion;

    public RepositorioOperacion getRepositorioOperacion() {return repositorioOperacion;}

    public void setRepositorioOperacion(RepositorioOperacion repositorioOperacion) {
        this.repositorioOperacion = repositorioOperacion;
    }
    // Other fields and methods
}

@Dependent
@Stateless
@LocalBean
public class RepositorioOperacion extends RepositorioGenerico<Operacion> {
    // This class doesn't have injection points
    // just put it here for the reference made in the error message
}

父类 RepositorioGenerico< Menu> 只是一个具有一些与数据库和<$ c进行交互的通用方法的类$ c> Menu 是一个实体类。接口如下:

The parent class RepositorioGenerico<Menu> is just a class with some generic methods to interact with the DB and Menu is an entity class. The interfaces are as follow:

@Local
public interface GestorPersonas {
    // methods definitions, implemented by GestorPersonasImpl
}

@Local
public interface GestorUsuarios {
    // methods definitions, implemented by GestorUsuariosImpl
}

每个注入点只有一个可注入类,所以我不明白为什么注入失败。我不知道是否需要有关我的代码的更多详细信息,因此请问我,我将对其进行编辑;我只写了我认为需要的部分。

I have only one injectable class for each injection point so I don't understand why the injection fails. I don't know if more details about my code is needed, so ask me and I'll edit the post; I write only the parts I think it's needed.

我看过另一篇文章,但是这里使用了 @Produces 注释, AFAIK,在这种情况下我不需要。

I've seen another post but there the @Produces annotation is used and, AFAIK, I don't need that for this case.

任何帮助或指导都将不胜感激。预先感谢您的回答

Any help or guide is appreciated. Thanks in advance for your answers

我在 Java杂志中的= nofollow noreferrer> Antonio Goncalves a> CDI是通过属性(甚至私有),setter方法或构造函数生成的。考虑到这一点,我删除了每个注入属性的getter和setter并确定了对这些属性的默认(包)的访问。

I read in an article by Antonio Goncalves in the Java Magazine that CDI is made via properties (even private), setter method or constructor. Taking that into account I removed getters and setters for every injected property and settled the access to default (package) for those properties.

注入属性的getter / setter方法仅用于设置

The getter/setter method for injected properties were only used to set the dependencies manually in my unit tests, so there's no need for them in production.

在所有这些更改之后,错误仍然存​​在...

After all this changes, the error remains...

根据 Java EE 7官方文档,我的bean符合CDI托管Bean的定义,除了要点

According to the Java EE 7 official documentation, my beans are compliant with the CDI Managed Bean definition, except for the point


没有注释带有EJB组件定义的注释或在ejb-jar.xml中声明为EJB bean类。

It is not annotated with an EJB component-defining annotation or declared as an EJB bean class in ejb-jar.xml.

根据 javax.ejb javadoc EJB组件定义ining注释是以下内容之一: @MessageDriven @Stateful @Stateless @Singleton 。但是我看到了其他使用CDI与EJB一起工作的项目(例如,将 @Stateless bean注入了其他地方)。另一方面,我一直在阅读 Java EE 7开始由Antonio Goncalves撰写,并说(第2章-上下文和依赖注入):

According to javax.ejb javadoc EJB component-defining annotation is one of the followings: @MessageDriven, @Stateful, @Stateless or @Singleton. But I've seen other projects with CDI working with EJB (e.g., @Stateless beans being injected somewhere else). On the other hand, I've been reading Beginning Java EE 7 by Antonio Goncalves and there says (Ch. 2 - Context and Dependency Injection):


CDI Bean的剖析

根据CDI 1.1规范,容器将满足以下条件的任何类视为CDI Bean:

According to the CDI 1.1 specification, the container treats any class that satisfies the following conditions as a CDI Bean:


  • 这不是一个非静态内部类,

  • 它是一个具体类,或者带有@Decorator批注,并且

  • 它具有一个没有参数的默认构造函数,或者声明带有@Inject注释的构造函数。

然后,bean可以具有可选范围,可选EL名称,集合拦截器绑定和可选的生命周期管理。

Then a bean can have an optional scope, an optional EL name, a set of interceptor bindings, and an optional life-cycle management.

我的bean符合该定义。我检查了焊接文档第2章带来了CDI bean的定义,该定义更加接近到第一个定义(来自Java EE文档)。正如我所说,问题是,我看到其他项目使用EJB托管Bean( @Stateless 或其他项目)进行依赖项注入。

And my beans are compliant with that definition. I checked the Weld Documentation and in chapter 2 brings the definition of a CDI bean, which is closer to the first definition (from the Java EE documentation). The thing is, as I said, that I've seen other projects making dependency injection with EJB Managed Beans (@Stateless or some other).

总而言之,我不知道该相信谁。

In conclusion, I don't know who to believe at. Can someone shed some light in this matter?

注意:在此尝试之后,我遇到了同样的问题

NOTE: After this attempt I have the same problem in a different place, so the server log and the involved classes are changed from the stated above, check the differences.

首先,在预感之后,我向需要的bean添加了接口注入到其他地方。

First, following a hunch, I added interfaces to the beans that I need to inject somewhere else.

第二,我将 Glassfish服务器从4.1更新为4.1.1 。这已经捆绑了 WELD-2.2.13.Final CDI实现(根据服务器日志消息 INFO:WELD-000900:2.2.13(最终))。

Second, I updated my Glassfish Server from 4.1 to 4.1.1. This one has bundled the WELD-2.2.13.Final CDI implementation (according to the server log message INFO: WELD-000900: 2.2.13 (Final)).

最后,我尝试部署我的项目,但该错误仍然存​​在一些更改。在显示更改之前,我要说它从未引起我的注意,但是在部署时,JNDI为我的bean生成了2个名称,如以下在尝试部署应用程序时抛出的日志消息中所述:

Finally I tried to deploy my project and the error remains with some changes. Before showing the changes, let me say that it never caught my attention but at deployment time, JNDI generates 2 names for my beans, as states in the following log message thrown when I try to deploy the application:

INFO:   Portable JNDI names for EJB RepositorioUsuarioImpl: [java:global/sigea-ear-0.8-SNAPSHOT/sigea-model-0.8-SNAPSHOT/RepositorioUsuarioImpl, java:global/sigea-ear-0.8-SNAPSHOT/sigea-model-0.8-SNAPSHOT/RepositorioUsuarioImpl!ar.edu.unt.sigea.repositorio.RepositorioUsuario]
INFO:   Portable JNDI names for EJB RepositorioAlumnoImpl: [java:global/sigea-ear-0.8-SNAPSHOT/sigea-model-0.8-SNAPSHOT/RepositorioAlumnoImpl, java:global/sigea-ear-0.8-SNAPSHOT/sigea-model-0.8-SNAPSHOT/RepositorioAlumnoImpl!ar.edu.unt.sigea.repositorio.RepositorioAlumno]
INFO:   Portable JNDI names for EJB RepositorioMenuImpl: [java:global/sigea-ear-0.8-SNAPSHOT/sigea-model-0.8-SNAPSHOT/RepositorioMenuImpl, java:global/sigea-ear-0.8-SNAPSHOT/sigea-model-0.8-SNAPSHOT/RepositorioMenuImpl!ar.edu.unt.sigea.repositorio.RepositorioMenu]
INFO:   Portable JNDI names for EJB GestorUsuariosImpl: [java:global/sigea-ear-0.8-SNAPSHOT/sigea-model-0.8-SNAPSHOT/GestorUsuariosImpl, java:global/sigea-ear-0.8-SNAPSHOT/sigea-model-0.8-SNAPSHOT/GestorUsuariosImpl!ar.edu.unt.sigea.servicios.GestorUsuarios]
INFO:   Portable JNDI names for EJB GestorPersonasImpl: [java:global/sigea-ear-0.8-SNAPSHOT/sigea-model-0.8-SNAPSHOT/GestorPersonasImpl!ar.edu.unt.sigea.servicios.GestorPersonas, java:global/sigea-ear-0.8-SNAPSHOT/sigea-model-0.8-SNAPSHOT/GestorPersonasImpl]
INFO:   Portable JNDI names for EJB RepositorioPersonaImpl: [java:global/sigea-ear-0.8-SNAPSHOT/sigea-model-0.8-SNAPSHOT/RepositorioPersonaImpl, java:global/sigea-ear-0.8-SNAPSHOT/sigea-model-0.8-SNAPSHOT/RepositorioPersonaImpl!ar.edu.unt.sigea.repositorio.RepositorioPersona]
INFO:   Portable JNDI names for EJB RepositorioOperacionImpl: [java:global/sigea-ear-0.8-SNAPSHOT/sigea-model-0.8-SNAPSHOT/RepositorioOperacionImpl!ar.edu.unt.sigea.repositorio.RepositorioOperacion, java:global/sigea-ear-0.8-SNAPSHOT/sigea-model-0.8-SNAPSHOT/RepositorioOperacionImpl]

我不知道这是否可能导致注入点错误,但听起来可疑(而且我不知道如何更改)。它也是在第一次尝试中出现的,但正如我所说,它从未引起我的注意。现在,日志和类

I don't know if that could be causing the error in the injection point but sounds suspicious (and I don't know how to change that). It appeared in the first attempt too but, as I said, it never caught my attention. Now the logs and the classes

Fatal:   Exception during lifecycle processing
org. glassfish.deployment.common.DeploymentException: CDI deployment failure:Exception List with 2 exceptions:
Exception 0 :
org.jboss.weld.exceptions.DeploymentException: WELD-001409: Ambiguous dependencies for type GestorPersonas with qualifiers @Default
  at injection point [BackedAnnotatedField] @Inject ar.edu.unt.sigea.inicio.RegistroBean.gestorPersonas
  at ar.edu.unt.sigea.inicio.RegistroBean.gestorPersonas(RegistroBean.java:0)
  Possible dependencies: 
  - Session bean [class ar.edu.unt.sigea.servicios.impl.GestorPersonasImpl with qualifiers [@Any @Default]; local interfaces are [GestorPersonas],
  - Managed Bean [class ar.edu.unt.sigea.servicios.impl.GestorPersonasImpl] with qualifiers [@Any @Default]
    ...[Stack trace]...

Exception 0 :
org.jboss.weld.exceptions.DeploymentException: WELD-001409: Ambiguous dependencies for type GestorUsuarios with qualifiers @Default
  at injection point [BackedAnnotatedField] @Inject ar.edu.unt.sigea.inicio.SesionUsuarioBean.gestorUsuarios
  at ar.edu.unt.sigea.inicio.SesionUsuarioBean.gestorUsuarios(SesionUsuarioBean.java:0)
  Possible dependencies: 
  - Session bean [class ar.edu.unt.sigea.servicios.impl.GestorUsuariosImpl with qualifiers [@Any @Default]; local interfaces are [GestorUsuarios],
  - Managed Bean [class ar.edu.unt.sigea.servicios.impl.GestorUsuariosImpl] with qualifiers [@Any @Default]
    ...[Stack trace]...


参与的课程


@Named(value = "registroBean")
@ViewScoped
public class RegistroBean implements Serializable {

    private static final long serialVersionUID = 2181909526300424451L;

    @Inject
    GestorPersonas gestorPersonas;

    // fields, methods, etc
}

@Named(value = "sesionUsuarioBean")
@SessionScoped
public class SesionUsuarioBean implements Serializable {

    private static final long serialVersionUID = 2938631472102116238L;

    @Inject
    GestorUsuarios gestorUsuarios;

    // fields, methods, etc
}

将显示注入的字段在主要部分中。

The injected fields are shown in the main section.

感谢您能为我提供此新信息的任何帮助。如果您需要有关该应用程序的更多信息,请告诉我。

Any help you can provide me with this new info is appreciated. If you need some more info about the application just let me know.

在此先感谢您的回答。

推荐答案

将互联网颠倒了2周之后,我终于尝试在 Wildfly 10.0.0 服务器上进行部署,现在我的应用程序部署就没有问题了。因此,我必须得出结论,这是关于Glassfish的错误。 Pitty,因为我使用的是最新版本(4.1.1),所以这是CDI发现的基本要求(我认为)。希望 Payara 解决此问题,但我暂时不会尝试。

After turning the internet upside down for 2 weeks, I finally tried to deploy on a Wildfly 10.0.0 Server and now my application deploys with no problem. So I must conclude that it's a bug on Glassfish. Pitty, because I'm using the lastest version (4.1.1) and it's something basic (I think) as CDI discovery. Hope that Payara solves this issue, but I'll not be trying it right now.

我必须说:如果服务器假定是符合Java EE 7 ,则不应发布带有此类错误的服务器。

I must say: if a server is suppose to be "Java EE 7 compliant", it should not be released with this kind of bugs.

如果某人知道某个论坛或不同服务器的一些基准测试结果,请注释掉。 让您感到羞耻的玻璃鱼

If someone knows about a discussion forum or knows some benchmark results for the different servers, please comment it out. Shame on you Glassfish

这篇关于Glassfish 4.1 CDI WELD-001409:含糊不清的依存关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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