我正在学习Java EE中的CDI和不同的注释。我创建了一个repo。我使用相同的托管bean来生成两个问候字符串。非正式和正式。我收到以下错误:
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)
我不确定为什么当第一个函数用Formal注释而第二个函数用Informal注释时会有歧义。当我删除其中一个函数或将其移动到另一个bean类时,错误消失了。谢谢
public class ProduceGreeting {
@Produces
@Formal
public String GetFormalGreeting(){
return "Good morning !";
}
@Produces
@InFormal
public String GetInFormalGreeting(){
return "Hi there !";
}
}
调用ProduceGreeting类的Servlet:
@Inject
@Formal
String formalMessage;
@Inject
@InFormal
String informalMessage;
...
...
out.println("<h2> Formal Greeting: " + formalMessage + "</h2>");
out.println("<h2> Informal Greeting: " + informalMessage + "</h2>");
编辑 - 添加非正式和正式注释(也可在repo中找到)。
@Qualifier
@Retention(RUNTIME)
@Target({TYPE, METHOD, FIELD, PARAMETER})
public @interface Formal {}
@Qualifier
@Retention(RUNTIME)
@Target({TYPE, METHOD, FIELD, PARAMETER})
public @interface InFormal {}
答案 0 :(得分:1)
错误消息显示您有
mypackage.ProduceFormalGreeting.GetFormalGreeting()
方法和
mypackage.ProduceGreeting.GetFormalGreeting()
也是。我想你需要在target
目录中进行清理以删除旧类(它可能在开发期间被重命名)。 (代码很好,它适用于Java SE。)