gpt4 book ai didi

jakarta-ee - 同一个 bean 中的两个生产者导致歧义错误

转载 作者:行者123 更新时间:2023-12-03 09:50:39 25 4
gpt4 key购买 nike

我正在学习 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 {}

最佳答案

错误消息说你有一个

mypackage.ProduceFormalGreeting.GetFormalGreeting()

方法和一个
mypackage.ProduceGreeting.GetFormalGreeting()

以及。我猜你需要清理 target删除旧类的目录(它可能在开发过程中被重命名)。 (代码很好,它适用于 Java SE。)

关于jakarta-ee - 同一个 bean 中的两个生产者导致歧义错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23615224/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com