CDI Ambiguous dependencies
Asked Answered
O

4

17

I have a @SessionScoped @Named bean with a @Producer method for a user object:

@Named @SessionScoped
public class UserBean implements Serializable
{
  //...
  @Named @Produces @LoggedIn @SessionScoped
  public MyUser getCurrentUser() {return user;}
}

This works fine in my setup (JBoss-7.1.1-Final) and it's no problem to access the user fields from JSF pages with #{currentUser.name}. The qualifier is org.jboss.seam.security.annotations.LoggedIn. Now I want to @Inject this user in a field in another @Named Bean:

@Named
public class FavBean implements Serializable
{   
  private @Inject @LoggedIn MyUser currentUser;
}

This gives me the error:

org.jboss.weld.exceptions.DeploymentException:
WELD-001409 Ambiguous dependencies for type [MyUser] with qualifiers [@Default] at
  injection point [[field] @Inject @LoggedIn test.FavBean.currentUser].
Possible dependencies [[Managed Bean [class test.ejb.MyUser] with qualifiers
  [@Any @Default],
Producer Method [MyUser] with qualifiers [@Any @Default] declared as [[method]
  @Named @Produces @LoggedIn @SessionScoped public test.UserBean.getCurrentUser()]]]

I don't understand the first dependency Managed Bean [class test.ejb.MyUser] This class is a simple @Entity and deployed in an ebb.jar in a EAR. As a workaround I'm currently injecting the UserBean get the user from there.

Ormiston answered 17/4, 2012 at 6:4 Comment(0)
M
22

This is because CDI searches for beans by type and your entity and the producer method return the same type. That's why it is ambiguous.

You need to define a new qualifier and annotate it with your producer method.

@Qualifier
@Retention(RUNTIME)
@Target({METHOD, FIELD, PARAMETER, TYPE})
public @interface CurrentUser {
}

Add this annotation to your producer method:

@Named @Produces @CurrentUser @LoggedIn @SessionScoped
public MyUser getCurrentUser() {return user;}
Malonis answered 17/4, 2012 at 6:44 Comment(1)
Or modify LoggedIn annotation by adding Qualifier to it. It seems it doesn't have Qualifier.Demography
H
5

I had very similar problem, and I got some offline help. My problem was that where my service was, it was included in a deployed ear AND in my web project as well. It was an accidental duplication, drop one out, and it will work if it is your case as well.

here on the following picture I had the esb_khr inside the esb_khr_web, I removed. In eclipse: go to properties and deployment assembly.

enter image description here

Hokeypokey answered 19/3, 2013 at 10:39 Comment(0)
L
4

I'm not an expert but I had a similar problem, and I fixed it in simpler way, by annotating the bean itself with @Alternative, so that the Producer is favored. Could be I'm missing some side effects but this worked as far as I could see / needed.

Lurlinelusa answered 21/7, 2016 at 20:45 Comment(1)
Thanks a lot. Everywhere this @Alternative annotation seem to have been totally forgotten.Nereidanereids
E
2

Please double check that you do not have multiple instances of beans.xml in your context. In my case I had beans.xml in WEB-INF and in META-INF. After removing beans.xml from META-INF, the similar issue got resolved.

Eugenie answered 23/1, 2020 at 18:55 Comment(1)
can't thank you enough! i had beans.xml in test resources folder and in normal resources folder as well, and my entity manager got instantiated twice and in memory database for tests probably as well, and tests were failing very randomly which drove me nutsFolkways

© 2022 - 2024 — McMap. All rights reserved.