Two manufacturers in the same bean cause Ambiguity error

I am learning CDI in Java EE and various annotations. I created a repository. I use the same managed bean to create two welcome lines. Informal and formal. I get the 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'm not sure why there is ambiguity when the first function is annotated with Formal and the second with Informal. The error is deleted when I delete one of the functions or redirect it to another bean class. Thanks you

public class ProduceGreeting {

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

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

Servlet calling 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 - Add informal and formal annotations (also available in repo ).

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


@Qualifier
@Retention(RUNTIME)
@Target({TYPE, METHOD, FIELD, PARAMETER})
public @interface InFormal {}
+4
source share
1 answer

The error message states that you have

mypackage.ProduceFormalGreeting.GetFormalGreeting()

and

mypackage.ProduceGreeting.GetFormalGreeting()

. , target, (, , ). ( , Java SE.)

+1

Source: https://habr.com/ru/post/1540264/


All Articles