Java EE – CDI: weld-001408 dissatisfied dependencies, how to solve them?

I do a small test project of CDI My application consists of EJB ear and war, all deployed on GlassFish 4 I use hibernate 4.3 4. Access the database

My goal is to verify that classes in EJB (DAO) can receive the injection of entitymanager

The pattern of sessionbean EJB is not great, but I have to modify the created application, so I don't have much choice

This is my EJB code:

@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

@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();

    }


}

And servicelocal java

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

In my war

@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,IOException {
        // TODO Auto-generated method stub
    }


 }

I annotated daotest by using @ stateless annotation Everything should happen So CDI works well, but as long as @ naming it doesn't want to work

Any ideas?

Stack trace:

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

<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>

Solution

By default, Java EE 7 has an implicit bean archive, that is, bean classes need to discover range annotations as CDI beans

@Named is not a scope comment Try @ dependent instead

Beans are no longer required in CDI 1.1 / Java EE 7 xml. If you have one, the exact version is different from bean discovery mode Please refer to the bean archives section of CDI 1.1 specification

Because you didn't publish your beans XML, it's hard to say whether this file is part of the problem

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>