Olá pessoal, estou com um erro chato apresentado no console do eclipse. O problema é que o Wildfly não injeta o CDI do DAO genérico. Tem apresentando erro do tipo: "WELD-001408: Unsatisfied dependencies for type LivroDao with qualifiers @Default".
[...]
11:38:03,311 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-8) MSC000001: Failed to start service jboss.deployment.unit."LivrariaWildFly.war".WeldStartService: org.jboss.msc.service.StartException in service jboss.deployment.unit."LivrariaWildFly.war".WeldStartService: Failed to start service
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1904)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.jboss.weld.exceptions.DeploymentException: Exception List with 3 exceptions:
Exception 0 :
org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type LivroDao with qualifiers @Default
at injection point [BackedAnnotatedField] @Inject private br.projec.livraria.controller.VendasBean.livroDao
at br.projec.livraria.controller.VendasBean.livroDao(VendasBean.java:0)
at org.jboss.weld.bootstrap.Validator.validateInjectionPointForDeploymentProblems(Validator.java:359)
at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:281)
at org.jboss.weld.bootstrap.Validator.validateGeneralBean(Validator.java:134)
at org.jboss.weld.bootstrap.Validator.validateRIBean(Validator.java:155)
at org.jboss.weld.bootstrap.Validator.validateBean(Validator.java:518)
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:63)
at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:56)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
at org.jboss.threads.JBossThread.run(JBossThread.java:320)
Exception 1 :
org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type UsuarioDao with qualifiers @Default
at injection point [BackedAnnotatedField] @Inject private br.projec.livraria.controller.LoginBean.usuarioDao
at br.projec.livraria.controller.LoginBean.usuarioDao(LoginBean.java:0)
at org.jboss.weld.bootstrap.Validator.validateInjectionPointForDeploymentProblems(Validator.java:359)
at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:281)
at org.jboss.weld.bootstrap.Validator.validateGeneralBean(Validator.java:134)
at org.jboss.weld.bootstrap.Validator.validateRIBean(Validator.java:155)
at org.jboss.weld.bootstrap.Validator.validateBean(Validator.java:518)
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:63)
at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:56)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
at org.jboss.threads.JBossThread.run(JBossThread.java:320)
Exception 2 :
org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type AutorDao with qualifiers @Default
at injection point [BackedAnnotatedField] @Inject private br.projec.livraria.controller.LivroBean.autorDao
at br.projec.livraria.controller.LivroBean.autorDao(LivroBean.java:0)
at org.jboss.weld.bootstrap.Validator.validateInjectionPointForDeploymentProblems(Validator.java:359)
at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:281)
at org.jboss.weld.bootstrap.Validator.validateGeneralBean(Validator.java:134)
at org.jboss.weld.bootstrap.Validator.validateRIBean(Validator.java:155)
at org.jboss.weld.bootstrap.Validator.validateBean(Validator.java:518)
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:63)
at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:56)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
at org.jboss.threads.JBossThread.run(JBossThread.java:320)
at org.jboss.weld.bootstrap.ConcurrentValidator.validateBeans(ConcurrentValidator.java:76)
at org.jboss.weld.bootstrap.Validator.validateDeployment(Validator.java:479)
at org.jboss.weld.bootstrap.WeldStartup.validateBeans(WeldStartup.java:445)
at org.jboss.weld.bootstrap.WeldBootstrap.validateBeans(WeldBootstrap.java:90)
at org.jboss.as.weld.WeldStartService.start(WeldStartService.java:96)
at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1948)
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1881)
... 3 more
11:38:04,354 INFO [org.primefaces.webapp.PostConstructApplicationEventListener] (ServerService Thread Pool -- 64) Running on PrimeFaces 6.0
11:38:04,361 INFO [org.wildfly.extension.undertow] (ServerService Thread Pool -- 64) WFLYUT0021: Registered web context: /Livraria-0.0.1-SNAPSHOT
11:38:04,367 ERROR [org.jboss.as.controller.management-operation] (Controller Boot Thread) WFLYCTL0013: Operation ("deploy") failed - address: ([("deployment" => "LivrariaWildFly.war")]) - failure description: {
"WFLYCTL0080: Failed services" => {"jboss.deployment.unit.\"LivrariaWildFly.war\".WeldStartService" => "org.jboss.msc.service.StartException in service jboss.deployment.unit.\"LivrariaWildFly.war\".WeldStartService: Failed to start service
Caused by: org.jboss.weld.exceptions.DeploymentException: Exception List with 3 exceptions:
Exception 0 :
org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type LivroDao with qualifiers @Default
at injection point [BackedAnnotatedField] @Inject private br.projec.livraria.controller.VendasBean.livroDao
at br.projec.livraria.controller.VendasBean.livroDao(VendasBean.java:0)
at org.jboss.weld.bootstrap.Validator.validateInjectionPointForDeploymentProblems(Validator.java:359)
at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:281)
at org.jboss.weld.bootstrap.Validator.validateGeneralBean(Validator.java:134)
at org.jboss.weld.bootstrap.Validator.validateRIBean(Validator.java:155)
at org.jboss.weld.bootstrap.Validator.validateBean(Validator.java:518)
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:63)
at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:56)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
at org.jboss.threads.JBossThread.run(JBossThread.java:320)
Exception 1 :
org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type UsuarioDao with qualifiers @Default
at injection point [BackedAnnotatedField] @Inject private br.projec.livraria.controller.LoginBean.usuarioDao
at br.projec.livraria.controller.LoginBean.usuarioDao(LoginBean.java:0)
at org.jboss.weld.bootstrap.Validator.validateInjectionPointForDeploymentProblems(Validator.java:359)
at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:281)
at org.jboss.weld.bootstrap.Validator.validateGeneralBean(Validator.java:134)
at org.jboss.weld.bootstrap.Validator.validateRIBean(Validator.java:155)
at org.jboss.weld.bootstrap.Validator.validateBean(Validator.java:518)
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:63)
at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:56)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
at org.jboss.threads.JBossThread.run(JBossThread.java:320)
Exception 2 :
org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type AutorDao with qualifiers @Default
at injection point [BackedAnnotatedField] @Inject private br.projec.livraria.controller.LivroBean.autorDao
at br.projec.livraria.controller.LivroBean.autorDao(LivroBean.java:0)
at org.jboss.weld.bootstrap.Validator.validateInjectionPointForDeploymentProblems(Validator.java:359)
at org.jboss.weld.bootstrap.Validator.validateInjectionPoint(Validator.java:281)
at org.jboss.weld.bootstrap.Validator.validateGeneralBean(Validator.java:134)
at org.jboss.weld.bootstrap.Validator.validateRIBean(Validator.java:155)
at org.jboss.weld.bootstrap.Validator.validateBean(Validator.java:518)
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:63)
at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:56)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
at org.jboss.threads.JBossThread.run(JBossThread.java:320)
"},
"WFLYCTL0412: Required services that are not installed:" => ["jboss.deployment.unit.\"LivrariaWildFly.war\".WeldStartService"],
"WFLYCTL0180: Services with missing/unavailable dependencies" => undefined
}
11:38:04,435 INFO [org.jboss.as.server] (ServerService Thread Pool -- 34) WFLYSRV0010: Deployed "Loja-0.0.1-SNAPSHOT.war" (runtime-name : "Loja-0.0.1-SNAPSHOT.war")
11:38:04,496 INFO [org.jboss.as.server] (ServerService Thread Pool -- 34) WFLYSRV0010: Deployed "LivrariaWildFly.war" (runtime-name : "LivrariaWildFly.war")
11:38:04,539 INFO [org.jboss.as.server] (ServerService Thread Pool -- 34) WFLYSRV0010: Deployed "Livraria-0.0.1-SNAPSHOT.war" (runtime-name : "Livraria-0.0.1-SNAPSHOT.war")
11:38:04,548 INFO [org.jboss.as.controller] (Controller Boot Thread) WFLYCTL0183: Service status report
WFLYCTL0186: Services which failed to start: service jboss.deployment.unit."LivrariaWildFly.war".WeldStartService: org.jboss.msc.service.StartException in service jboss.deployment.unit."LivrariaWildFly.war".WeldStartService: Failed to start service
[...]
Segue os daos específicos:
package br.project.livraria.dao;
import java.io.Serializable;
import javax.enterprise.context.ApplicationScoped;
import javax.persistence.EntityManager;
import br.project.livraria.model.Autor;
@ApplicationScoped
public class AutorDao extends GenericDao<Autor, Long> implements Serializable {
private static final long serialVersionUID = 1L;
public AutorDao(EntityManager em, Class<Autor> classe) {
super(classe);
// TODO Auto-generated constructor stub
}
}
package br.project.livraria.dao;
import java.io.Serializable;
import javax.enterprise.context.ApplicationScoped;
import javax.persistence.EntityManager;
import br.project.livraria.model.Livro;
@ApplicationScoped
public class LivroDao extends GenericDao<Livro, Long> implements Serializable {
private static final long serialVersionUID = 1L;
public LivroDao(EntityManager em, Class<Livro> classe) {
super(classe);
// TODO Auto-generated constructor stub
}
}
package br.project.livraria.dao;
import java.io.Serializable;
import javax.enterprise.context.ApplicationScoped;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.TypedQuery;
import br.project.livraria.model.Usuario;
@ApplicationScoped
public class UsuarioDao extends GenericDao<Usuario, Long> implements Serializable{
private static final long serialVersionUID = 1L;
public UsuarioDao(EntityManager em, Class<Usuario> classe) {
super(classe);
// TODO Auto-generated constructor stub
}
@SuppressWarnings("unused")
public boolean existe(Usuario usuario) {
EntityManager em = new JPAUtil().getEntityManager();
TypedQuery<Usuario> query = em.createQuery(
" select u from Usuario u "
+ " where u.email = :pEmail and u.senha = :pSenha", Usuario.class);
query.setParameter("pEmail", usuario.getEmail());
query.setParameter("pSenha", usuario.getSenha());
try {
Usuario resultado = query.getSingleResult();
} catch (NoResultException ex) {
return false;
}
em.close();
return true;
}
}
Agora segue o dão generico:
package br.project.livraria.dao;
import java.io.Serializable;
import java.util.List;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaQuery;
public class GenericDao<T, K> implements Serializable {
private static final long serialVersionUID = 1L;
private final Class<T> classe;
@PersistenceContext(unitName="livraria")
@Inject
private EntityManager em;
public GenericDao(Class<T> classe) {
this.classe = classe;
}
public void adiciona(T t) {
// persiste o objeto
em.persist(t);
}
public void remove(T t) {
em.remove(em.merge(t));
}
public void atualiza(T t) {
em.merge(t);
}
public List<T> listaTodos() {
CriteriaQuery<T> query = em.getCriteriaBuilder().createQuery(classe);
query.select(query.from(classe));
List<T> lista = em.createQuery(query).getResultList();
return lista;
}
public T buscaPorId(K id) {
T instancia = em.find(classe, id);
return instancia;
}
public int contaTodos() {
long result = (Long) em.createQuery("select count(n) from livro n")
.getSingleResult();
return (int) result;
}
public List<T> listaTodosPaginada(int firstResult, int maxResults) {
CriteriaQuery<T> query = em.getCriteriaBuilder().createQuery(classe);
query.select(query.from(classe));
List<T> lista = em.createQuery(query).setFirstResult(firstResult)
.setMaxResults(maxResults).getResultList();
return lista;
}
@Produces
public EntityManager getEm() {
return em;
}
}
Eu resolvi deixar o container fazer as trasações necessárias e configurei o pool de conexões no servido Wildfly 10.1. Então todo e qualquer trasação desverá ser feita pelo container. Isso está referenciado no persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="livraria" transaction-type="JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<non-jta-data-source>java:/Livraria</non-jta-data-source>
<class>br.project.livraria.model.Autor</class>
<class>br.project.livraria.model.Livro</class>
<class>br.project.livraria.model.Usuario</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="hibernate.connection.shutdown" value="true" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
</properties>
</persistence-unit>
</persistence>
No entanto, o cdi está dando erro no dão genérico, ou seja, ele não reconhece como já tinha visto em alguns postagens por aqui. Agora, não consigo resolver esse problema. Eu não sei se o erro é de configuração (anotações erradas em locais errados) ou se de fato, é necessário uma outra classe que faça o devido gerenciamento entre o dão genérico e o dao específico. Alguém poderia mim dar uma dica ou mesmo o procedimento correto de configuração?