CDI : WELD-001408 Unsatisfied dependencies, how to resolve it?
Asked Answered
L

4

23

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.

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

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

Here is my code of the 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

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

And inside 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, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }


 }

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.

Any idea?

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

<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>
Ludicrous answered 5/3, 2014 at 7:32 Comment(0)
F
17

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 is not a scope annotation. Try @Dependent instead.

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.

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

Femoral answered 5/3, 2014 at 18:26 Comment(2)
What is the version of CDI embedded with Glassfish 4?Ludicrous
Ok it's works. Like you told, it was missing the Scope tag on DAOTest.java. Really thank you.Ludicrous
A
14

Since this is the first hit I got searching for WELD-001408, let me also mention one cause which is the lack of a no-arg constructor. This was apparently NOT the case for the OP but it was the cause of the problem in my own case, so this may help others too.

Audreaaudres answered 8/9, 2015 at 19:15 Comment(1)
Still a relevant answer in 2020 w/ CDI 2 & Weld 3. Worked for me today.Whitehurst
P
1

@LocalBean means you will inject the bean and not the interface

@Inject Service service

and not

@Inject ServiceLocal service
Pirouette answered 12/12, 2016 at 19:30 Comment(0)
L
0

Seems like DAOTest's archive has no beans.xml / misplaced beans.xml.

Litmus answered 5/3, 2014 at 11:55 Comment(1)
I've placed the beans.xml into the META-INF of EJB project and into the WEB-INF of WAR project. DAOTest is placed into the same project that Service.java. Is it possible that the container that manage EntityManager is different of the CDI container. So CDI can't inject the EntityManager with @PersistenceContext?Ludicrous

© 2022 - 2024 — McMap. All rights reserved.