gpt4 book ai didi

java - Glassfish 4,CDI 中的简单示例因 WELD-001408 Unsatisfied dependencies 而失败

转载 作者:行者123 更新时间:2023-11-29 09:39:16 25 4
gpt4 key购买 nike

我是 CDI 的新手。这是我的第一个示例,我正在尝试运行它。在互联网上搜索后,我编写了以下代码:我要注入(inject)的类

public class Temp {

public Temp(){

}

public String getMe(){
return "something";
}
}

小服务程序

@WebServlet(name = "NewServlet", urlPatterns = {"/NewServlet"})
public class NewServlet extends HttpServlet {

@Inject
public Temp temp;

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
out.println("<body>");
out.println("<h1> Here it is"+temp.getMe()+ "</h1>");
out.println("</body>");
}
}
...

但我必须在 glassfish 4 中出现以下错误:

org.jboss.weld.exceptions.DeploymentException: WELD-001408 Unsatisfied dependencies for type [Temp] with qualifiers [@Default] at injection point [[BackedAnnotatedField] @Inject private xxx.example.NewServlet.temp]

我做错了什么?

最佳答案

WEB-INF 中不存在 beans.xml,或者文件需要将 bean-discovery-mode="annotated" 更改为 bean-discovery-mode="all".


<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
bean-discovery-mode="all">
</beans>

说明

推荐值“annotated”仅识别带注释的 CDI 托管 beans。没有任何注释的 Bean 将被忽略。由于您的 Temp 类不是 CDI bean,因此建议不适用于您的情况。

使用 bean-discovery-mode="annotated"

要使用 annotated,请使用 @RequestScoped 注释类:

// Import only this RequestScoped
import javax.enterprise.context.RequestScoped;

@RequestScoped
public class Temp {

public Temp() { }

public String getMe() {
return "something";
}
}

RequestScoped 会将您的 Temp 类转换为 CDI bean 并将与 bean-discovery-mode="annotated ".

关于java - Glassfish 4,CDI 中的简单示例因 WELD-001408 Unsatisfied dependencies 而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19899920/

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