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