CDI Ambiguous dependencies

后端 未结 4 895
孤城傲影
孤城傲影 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:33

    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.

    0 讨论(0)
  • 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;}
    
    0 讨论(0)
  • 2021-02-19 11:52

    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

    0 讨论(0)
  • 2021-02-19 11:53

    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.

    0 讨论(0)
提交回复
热议问题