本文介绍了CDI:WELD-001408不满意的依赖关系,如何解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用CDI做了一个小测试项目。我的应用程序由EJB EAR和WAR组成,全部部署在Glassfish 4上。我正在使用Hibernate 4.3.4来访问数据库。

I do a small test project with CDI. My application is composed of an EJB EAR and WAR, all deployed on Glassfish 4. I'm using Hibernate 4.3.4 to access the database.

我的目标是验证EJB(DAO)中的类可以接收EntityManager的注入。

My goal is to verify that a class in an EJB (DAO) can receive an injection of an EntityManager.

模式SessionBean + EJB不是很棒,但我必须修改已经创建的应用程序我没有太多选择。

The pattern SessionBean + EJB is not fantastic but I have to modify an application already created so I do not have much choice.

这是我的EJB代码:

@Named
public class DAOTest implements Serializable {
    private static final long serialVersionUID = 1L;

    @PersistenceContext(unitName="CDI-ejb")
    private EntityManager em;

    public void test(){
        //em.getClass();
    }


    public EntityManager getEm() {
       return em;
    }


    public void setEm(EntityManager em) {
        this.em = em;
    }

    public DAOTest() {
        // TODO Auto-generated constructor stub
    }

}

Service.java

Service.java

@Stateless
@LocalBean
public class Service implements ServiceLocal {

    @Inject DAOTest test;
    /**
    * Default constructor.
    */
    public Service() {
        // TODO Auto-generated constructor stub
    }


    @Override
    public void test() {
        test.test();

    }


}

和ServiceLocal.java

and ServiceLocal.java

@Local
public interface ServiceLocal {
    void test();
}

在我的战争中:

@WebServlet("/Test")
public class Test extends HttpServlet {
    private static final long serialVersionUID = 1L;
    @Inject private ServiceLocal service;

    /**
    * @see HttpServlet#HttpServlet()
    */
    public Test() {
        super();
        // TODO Auto-generated constructor stub
    }


    /**
    * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        service.test();
    }


    /**
    * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }


 }

我通过使用@Stateless注释对其进行注释来测试DAOTest。一切都按照预期发生。所以CDI效果很好。但只有@ Named它不想工作。

I tested the DAOTest by annotating it with @ Stateless annotation. Everything happens as it should. So CDI works well. But with just @ Named it does not want to work.

任何想法?

stacktrace:

The stacktrace:

org.jboss.weld.exceptions.DeploymentException: WELD-001408 Unsatisfied dependencies for type [DAOTest] with qualifiers [@Default] at injection point [[BackedAnnotatedField] @Inject test.Service.test]

My beans.xml

My beans.xml

<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
</beans>


推荐答案

Java EE 7默认具有隐式bean存档,即bean类需要将范围注释作为CDI bean发现。

Java EE 7 has implicit bean archives by default, i.e. a bean class requires a scope annotation to be discovered as CDI bean.

@Named 不是范围注释。请尝试 @Dependent

@Named is not a scope annotation. Try @Dependent instead.

beans.xml 是在CDI 1.1 / Java EE 7中不再需要。如果你有一个,那么确切的版本和 bean-discovery-mode 会有所不同。请参阅CDI 1.1规范的部分。

beans.xml is no longer required in CDI 1.1/Java EE 7. If you do have one, then the exact version and the bean-discovery-mode make a difference. See the Bean archives section of the CDI 1.1 spec.

由于您没有发布 beans.xml ,因此很难判断此文件是否属于问题。

As you didn't post your beans.xml, it's hard to tell whether or not this file is part of the problem.

这篇关于CDI:WELD-001408不满意的依赖关系,如何解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 04:02