gpt4 book ai didi

java - 没有bean符合注入(inject)到注入(inject)点的条件

转载 作者:搜寻专家 更新时间:2023-11-01 02:43:50 26 4
gpt4 key购买 nike

我正在尝试使用 CDI 将 DAO 注入(inject)我的 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不创造什么?我该如何纠正它?

最佳答案

实际上也请澄清你的beans.xml的内容,你可能需要给你的dao类添加一个作用域。

由于您使用的是带注释的,因此您还需要在您的 dao 上提供一个范围。它没有被拾取,因为它没有被注释。

@SuppressWarnings("serial")
@RequestScoped
public class ColaboradorDao implements Serializable{

然后它应该开始工作了。

关于java - 没有bean符合注入(inject)到注入(inject)点的条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27149388/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com