문제

I am learning CDI in Java EE and the different annotations. I created a repo. I am using the same managed bean to produce two greeting strings. Informal and Formal. I get following error:

SEVERE:   Exception while loading the app : CDI deployment failure:WELD-001409 Ambiguous dependencies for type [String] with qualifiers [@Formal] at injection point [[BackedAnnotatedField] @Inject @Formal mypackage.HelloServlet.formalMessage]. Possible dependencies [[Producer Method [String] with qualifiers [@Formal @Any] declared as [[BackedAnnotatedMethod] @Produces @Formal public mypackage.ProduceFormalGreeting.GetFormalGreeting()], Producer Method [String] with qualifiers [@Formal @Any] declared as [[BackedAnnotatedMethod] @Produces @Formal public mypackage.ProduceGreeting.GetFormalGreeting()]]]
org.jboss.weld.exceptions.DeploymentException: WELD-001409 Ambiguous dependencies for type [String] with qualifiers [@Formal] at injection point [[BackedAnnotatedField] @Inject @Formal mypackage.HelloServlet.formalMessage]. Possible dependencies [[Producer Method [String] with qualifiers [@Formal @Any] declared as [[BackedAnnotatedMethod] @Produces @Formal public mypackage.ProduceFormalGreeting.GetFormalGreeting()], Producer Method [String] with qualifiers [@Formal @Any] declared as [[BackedAnnotatedMethod] @Produces @Formal public mypackage.ProduceGreeting.GetFormalGreeting()]]]
    at org.jboss.weld.bootstrap.Validator.validateInjectionPointForDeploymentProblems(Validator.java:406)

I am not sure why there is ambiguity when the first function is annotated with Formal and the second with Informal. The error goes away when I remove one of the functions or move it to a different bean class. Thank you

public class ProduceGreeting {

    @Produces
    @Formal
    public String GetFormalGreeting(){
        return "Good morning !";
    }

    @Produces
    @InFormal
    public String GetInFormalGreeting(){
        return "Hi there !";
    }
}

Servlet that calls the ProduceGreeting class:

@Inject
@Formal
String formalMessage;

@Inject
@InFormal
String informalMessage;
...
...

 out.println("<h2> Formal Greeting: " + formalMessage + "</h2>");
 out.println("<h2> Informal Greeting: " + informalMessage + "</h2>");

EDIT - adding informal and formal annotations (also available in the repo).

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


@Qualifier
@Retention(RUNTIME)
@Target({TYPE, METHOD, FIELD, PARAMETER})
public @interface InFormal {}
도움이 되었습니까?

해결책

The error message says that you have a

mypackage.ProduceFormalGreeting.GetFormalGreeting()

method and a

mypackage.ProduceGreeting.GetFormalGreeting()

as well. I guess you need a cleanup in the target directory to remove the old class (it probably was renamed during development). (The code is fine, it works for me with Java SE.)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top