CDI Ambiguous dependencies

后端 未结 4 912
孤城傲影
孤城傲影 2021-02-19 11:11

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

@Named @SessionScoped
public class UserBean implements S         


        
4条回答
  •  死守一世寂寞
    2021-02-19 11:51

    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;}
    

提交回复
热议问题