java 没有bean符合注入到注入点的条件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27149388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
No bean is eligible for injection to the injection point
提问by prabello
I'm trying to use CDI to inject a DAO into my ManagedBean
我正在尝试使用 CDI 将 DAO 注入我的 ManagedBean
ManagedBean:
托管Bean:
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 My EntityManagerProducer:
@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
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 shows me a warning at the @Inject, saying:
Eclipse 在@Inject 向我显示了一个警告,说:
No bean is eligible for injection to the injection point [JSR-299 §5.2.1]
When i try to deploy it on WildFly:
当我尝试在 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.doWork(ConcurrentValidator.java:68)
at org.jboss.weld.bootstrap.ConcurrentValidator.doWork(ConcurrentValidator.java:66)
at org.jboss.weld.executor.IterativeWorkerTaskFactory.call(IterativeWorkerTaskFactory.java:60)
at org.jboss.weld.executor.IterativeWorkerTaskFactory.call(IterativeWorkerTaskFactory.java:53)
at java.util.concurrent.FutureTask.run(Unknown Source) [rt.jar:1.8.0_20]
... 3 more
I do have the beans.xml on my project, just to allow CDI to work.
我的项目中有 beans.xml,只是为了让 CDI 工作。
As ColaboradorDao is a class that belongs to me i don't have to use @Produces,right? Then why the CDI doesn't what to create? How can i correct it?
由于 ColaboradorDao 是属于我的一个类,所以我不必使用 @Produces,对吗?那么为什么 CDI 不创建什么?我该如何纠正?
回答by John Ament
Actually also please clarify the contents of your beans.xml
, it's possible you need to add a scope to your dao class.
实际上还请澄清您的 内容,您beans.xml
可能需要为您的 dao 类添加一个范围。
Since you're using annotated, you need to also provide a scope on your dao. It's not being picked up because it's not annotated.
由于您使用的是带注释的,因此您还需要在您的 dao 上提供一个范围。它没有被拾取,因为它没有注释。
@SuppressWarnings("serial")
@RequestScoped
public class ColaboradorDao implements Serializable{
Then it should start working.
然后它应该开始工作。