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>
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With