我尝试使用CDI将DAO注入我的ManagedBean
ManagedBean:
import java.io.Serializable;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;
import br.com.timbrasil.network.dao.ColaboradorDao;
import br.com.timbrasil.network.modelo.Colaborador;
@SuppressWarnings("serial")
@Named
@RequestScoped
public class LoginBean implements Serializable{
private Colaborador colaborador = new Colaborador();
@Inject
private ColaboradorDao dao;
DAO
import java.io.Serializable;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import br.com.timbrasil.network.modelo.Colaborador;
@SuppressWarnings("serial")
@RequestScoped
public class ColaboradorDao implements Serializable{
@Inject
private EntityManager manager;
public Colaborador buscaEmailESenha(Colaborador colaborador) {
String jqpl = "select c from Colaborador c where c.email = :pEmail and c.senha = :cSenha";
TypedQuery<Colaborador> query = this.manager.createQuery(jqpl, Colaborador.class);
query.setParameter("pEmail", colaborador.getEmail());
query.setParameter("pSenha", colaborador.getSenha());
colaborador = query.getSingleResult();
return colaborador;
}
}
@Edit 我的EntityManagerProducer:
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Disposes;
import javax.enterprise.inject.Produces;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
@ApplicationScoped
public class JPAUtil {
private static EntityManagerFactory emf = Persistence
.createEntityManagerFactory("blueMonster");
@Produces
@RequestScoped
public EntityManager getEntityManager() {
return emf.createEntityManager();
}
public void close(@Disposes EntityManager manager) {
manager.close();
}
}
的beans.xml
<?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"
version="1.1" bean-discovery-mode="annotated">
</beans>
Eclipse在@Inject上向我显示警告,说:
No bean is eligible for injection to the injection point [JSR-299 §5.2.1]
当我尝试在WildFly上部署它时:
15:04:41,551 INFO [org.hibernate.Version] (ServerService Thread Pool -- 51) HHH000412: Hibernate Core {4.3.7.Final}
15:04:41,551 INFO [org.hibernate.cfg.Environment] (ServerService Thread Pool -- 51) HHH000206: hibernate.properties not found
15:04:41,551 INFO [org.hibernate.cfg.Environment] (ServerService Thread Pool -- 51) HHH000021: Bytecode provider name : javassist
15:04:41,676 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-6) MSC000001: Failed to start service jboss.deployment.unit."network.war".WeldStartService: org.jboss.msc.service.StartException in service jboss.deployment.unit."network.war".WeldStartService: Failed to start service
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1904) [jboss-msc-1.2.2.Final.jar:1.2.2.Final]
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [rt.jar:1.8.0_20]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [rt.jar:1.8.0_20]
at java.lang.Thread.run(Unknown Source) [rt.jar:1.8.0_20]
Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type ColaboradorDao with qualifiers @Default
at injection point [BackedAnnotatedField] @Inject private br.com.timbrasil.network.mb.LoginBean.dao
at br.com.timbrasil.network.mb.LoginBean.dao(LoginBean.java:0)
at org.jboss.weld.bootstrap.Validator.validateInjectionPointForDeploymentProblems(Validator.java:372)
at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:293)
at org.jboss.weld.bootstrap.Validator.validateGeneralBean(Validator.java:134)
at org.jboss.weld.bootstrap.Validator.validateRIBean(Validator.java:167)
at org.jboss.weld.bootstrap.Validator.validateBean(Validator.java:531)
at org.jboss.weld.bootstrap.ConcurrentValidator$1.doWork(ConcurrentValidator.java:68)
at org.jboss.weld.bootstrap.ConcurrentValidator$1.doWork(ConcurrentValidator.java:66)
at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:60)
at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:53)
at java.util.concurrent.FutureTask.run(Unknown Source) [rt.jar:1.8.0_20]
... 3 more
我的项目中有beans.xml,只是为了让CDI正常工作。
由于ColaboradorDao是一个属于我的课程,我不必使用@ Produces,对吧? 那么为什么CDI没有创造什么? 我怎么能纠正它?
答案 0 :(得分:4)
实际上请澄清beans.xml
的内容,您可能需要在您的dao类中添加一个范围。
由于您正在使用注释,因此您还需要在dao上提供范围。它没有被提取,因为它没有注释。
@SuppressWarnings("serial")
@RequestScoped
public class ColaboradorDao implements Serializable{
然后它应该开始工作。