Thor Thor - 3 months ago 8
Java Question

CDI Ambiguous dependencies

I have a

@SessionScoped @Named
bean with a
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
. The qualifier is
. Now I want to
this user in a field in another

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

This gives me the error:

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
and deployed in an ebb.jar in a EAR. As a workaround I'm currently injecting the
get the user from there.


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.

public @interface CurrentUser {

Add this annotation to your producer method:

@Named @Produces @CurrentUser @LoggedIn @SessionScoped
public MyUser getCurrentUser() {return user;}