1

私はCDIの初心者です。これは私の最初の例であり、実行しようとしています。インターネットを検索して、次のコードを書きました: 注入したいクラス

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>");
    }
}
...

しかし、私はグラスフィッシュ4のエラーに従う必要があります:

org.jboss.weld.exceptions.DeploymentException: WELD-001408 インジェクション ポイント [[BackedAnnotatedField] @Inject private xxx.example.NewServlet.temp] で修飾子 [@Default] を使用したタイプ [Temp] の満たされていない依存関係

私は何を間違っていますか?

4

2 に答える 2

11

内にnobeans.xmlが存在するWEB-INFか、ファイルを に変更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マネージド Bean のみを認識します。アノテーションのない Bean は無視されます。あなたのTempクラスはCDIBean ではないため、推奨事項は適用されません。

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";
    }
}

これにより、クラスがBean RequestScopedに変換され、 .TempCDIbean-discovery-mode="annotated"

于 2013-11-11T06:25:50.853 に答える