Pessoal, boa tarde!
Eu estou com problemas com uma aplicação que comecei a fazer para aprender CDI e não sei o que pode ser…
me ajudem…
Segue as classes e o erro:
[code]package br.com.transportadora.dao;
import java.io.Serializable;
import java.util.List;
public interface Dao<T> extends Serializable {
public void save(T entity);
public T update(T entity);
public void delete(T entity);
public T find(Integer id);
public T findReference(Integer id);
public List<T> list();
} [/code]
[code]package br.com.transportadora.dao;
import java.util.List;
import br.com.transportadora.model.Usuario;
public interface UsuarioDao extends Dao<Usuario> {
public Usuario findByLoginSenha(String login, String senha);
public List<Usuario> listAdministradores();
public List<Usuario> listUsuariosPadrao();
}[/code]
[code]package br.com.transportadora.dao;
import java.util.List;
import br.com.transportadora.model.Usuario;
public interface UsuarioDao extends Dao<Usuario> {
public Usuario findByLoginSenha(String login, String senha);
public List<Usuario> listAdministradores();
public List<Usuario> listUsuariosPadrao();
}[/code]
[code]package br.com.transportadora.dao.imp;
import java.util.List;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import br.com.transportadora.dao.UsuarioDao;
import br.com.transportadora.model.Tipo;
import br.com.transportadora.model.Usuario;
import br.com.transportadora.qualifiers.UsuarioDaoCDI;
@UsuarioDaoCDI
public class UsuarioDaoImpl extends BaseDaoImpl<Usuario> implements UsuarioDao {
private static final long serialVersionUID = 1L;
@Inject
public UsuarioDaoImpl(EntityManager manager) {
super.manager = manager;
classePersistente = Usuario.class;
}
@Override
public Usuario findByLoginSenha(String login, String senha) {
CriteriaBuilder cb = manager.getCriteriaBuilder();
CriteriaQuery<Usuario> cq = cb.createQuery(classePersistente);
Root<Usuario> root = cq.from(classePersistente);
Predicate predicate = cb.and(cb.equal(root.get("login"), cb.parameter(String.class, "login")),cb.equal(root.get("senha"), cb.parameter(String.class, "senha")));
cq.where(predicate);
TypedQuery<Usuario> tq = manager.createQuery(cq);
tq.setParameter("login", login);
tq.setParameter("senha", senha);
return tq.getSingleResult();
}
@Override
public List<Usuario> listAdministradores() {
CriteriaBuilder cb = manager.getCriteriaBuilder();
CriteriaQuery<Usuario> cq = cb.createQuery(classePersistente);
Root<Usuario> root = cq.from(classePersistente);
cq.select(root);
Predicate predicate = cb.equal(root.get("tipo"), cb.parameter(Tipo.class, "tipo"));
cq.where(predicate);
TypedQuery<Usuario> tq = manager.createQuery(cq);
tq.setParameter("tipo", Tipo.ADMIN);
return tq.getResultList();
}
@Override
public List<Usuario> listUsuariosPadrao() {
CriteriaBuilder cb = manager.getCriteriaBuilder();
CriteriaQuery<Usuario> cq = cb.createQuery(classePersistente);
Root<Usuario> root = cq.from(classePersistente);
cq.select(root);
Predicate predicate = cb.equal(root.get("tipo"), cb.parameter(Tipo.class, "tipo"));
cq.where(predicate);
TypedQuery<Usuario> tq = manager.createQuery(cq);
tq.setParameter("tipo", Tipo.USER);
return tq.getResultList();
}
}
[/code]
[code]package br.com.transportadora.qualifiers;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.TYPE;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.inject.Qualifier;
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({TYPE, METHOD, FIELD, PARAMETER})
public @interface UsuarioDaoCDI {
}
[/code]
[code]package br.com.transportadora.util;
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;
@RequestScoped
public class EntityManagerProdutor {
@Produces @RequestScoped
public EntityManager getEntityManager() {
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("transportadora-pu");
EntityManager entityManager = entityManagerFactory.createEntityManager();
return entityManager;
}
public void disposeEntityManager(@Disposes EntityManager em) {
em.close();
}
}
[/code]
[code]package br.com.transportadora.service;
import java.io.Serializable;
import java.util.List;
public interface Service<T> extends Serializable {
public void save(T entity);
public T update(T entity);
public void delete(T entity);
public T find(Integer id);
public T findReference(Integer id);
public List<T> list();
}
[/code]
[code]package br.com.transportadora.service;
import java.util.List;
import br.com.transportadora.model.Usuario;
public interface UsuarioService extends Service<Usuario> {
public Usuario buscarPorLoginSenha(String login, String senha);
public List<Usuario> listAdministradores();
public List<Usuario> listUsuariosPadrao();
}[/code]
[code]package br.com.transportadora.service.impl;
import java.util.List;
import br.com.transportadora.dao.Dao;
import br.com.transportadora.service.Service;
public abstract class BaseServiceImpl<T> implements Service<T> {
private static final long serialVersionUID = 1L;
protected Dao<T> dao;
public void save(T entity) {
dao.save(entity);
}
public T update(T entity) {
return dao.update(entity);
}
public void delete(T entity) {
dao.delete(entity);
}
public T find(Integer id) {
return dao.find(id);
}
public T findReference(Integer id) {
return dao.findReference(id);
}
public List<T> list() {
return dao.list();
}
public void setDao(Dao<T> dao) {
this.dao = dao;
}
}
[/code]
[code]package br.com.transportadora.service.impl;
import java.util.List;
import javax.inject.Inject;
import br.com.transportadora.dao.Dao;
import br.com.transportadora.dao.UsuarioDao;
import br.com.transportadora.model.Usuario;
import br.com.transportadora.qualifiers.UsuarioDaoCDI;
import br.com.transportadora.qualifiers.UsuarioServiceCDI;
import br.com.transportadora.service.UsuarioService;
@UsuarioServiceCDI
public class UsuarioServiceImpl extends BaseServiceImpl<Usuario> implements UsuarioService {
private static final long serialVersionUID = 1L;
@Inject
public UsuarioServiceImpl(@UsuarioDaoCDI Dao<Usuario> dao) {
super.dao = dao;
}
@Override
public Usuario buscarPorLoginSenha(String login, String senha) {
return ((UsuarioDao) dao).findByLoginSenha(login, senha);
}
@Override
public List<Usuario> listAdministradores() {
return ((UsuarioDao) dao).listAdministradores();
}
@Override
public List<Usuario> listUsuariosPadrao() {
return ((UsuarioDao) dao).listUsuariosPadrao();
}
}
[/code]
[code]package br.com.transportadora.web.managedbeans;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.inject.Inject;
import br.com.transportadora.model.Usuario;
import br.com.transportadora.qualifiers.UsuarioServiceCDI;
import br.com.transportadora.service.Service;
import br.com.transportadora.service.UsuarioService;
@ManagedBean (name=“usuarioMB”)
@SessionScoped
public class UsuarioLoginMB {
private Usuario usuario;
@Inject @UsuarioServiceCDI
private Service<Usuario> service;
public void listarTeste() {
System.out.println("Entrou aqui!!!");
System.out.println(((UsuarioService) service).listAdministradores());
}
public void setUsuario(Usuario usuario) {
this.usuario = usuario;
}
public Usuario getUsuario() {
return usuario;
}
public void setService(Service<Usuario> service) {
this.service = service;
}
}
[/code]
[code]<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=“http://www.w3.org/1999/xhtml”
xmlns:h=“http://java.sun.com/jsf/html”
xmlns:f=“http://java.sun.com/jsf/core”
xmlns:ui=“http://java.sun.com/jsf/facelets”>
<h:head></h:head>
<body>
<h:form >
<h:commandButton action="#{usuarioMB.listarTeste()}" value=“Teste” />
</h:form>
</body>
</html>
[/code]
Erro quando executo:
26/08/2012 18:33:19 org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jdk1.6.0_33\jre\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Program Files\Common Files\Microsoft Shared\Windows Live;C:\Program Files (x86)\Common Files\Microsoft Shared\Windows Live;C:\Program Files (x86)\AMD APP\bin\x86_64;C:\Program Files (x86)\AMD APP\bin\x86;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static\;C:\Program Files (x86)\Sony\VAIO Startup Setting Tool;C:\Program Files (x86)\Windows Live\Shared;C:\Program Files\MySQL\MySQL Server 5.5\bin;C:\Program Files (x86)\Apache Software Foundation\apache-maven-3.0.4\bin;C:\Program Files (x86)\Apache Software Foundation\apache-ant-1.8.4\bin;C:\Program Files\TortoiseSVN\bin;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;C:\Program Files\Java\jdk1.6.0_33\bin;c:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\;c:\Program Files\Microsoft SQL Server\100\Tools\Binn\;c:\Program Files\Microsoft SQL Server\100\DTS\Binn\;c:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE\;c:\Program Files (x86)\Microsoft SQL Server\100\DTS\Binn\;C:\Ruby187\bin;.
26/08/2012 18:33:20 org.apache.tomcat.util.digester.SetPropertiesRule begin
AVISO: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:sistema-transportadora' did not find a matching property.
26/08/2012 18:33:20 org.apache.tomcat.util.digester.Digester endElement
AVISO: No rules found matching 'Server/Service/Engine/Host/Context/listener'.
26/08/2012 18:33:20 org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
26/08/2012 18:33:20 org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
26/08/2012 18:33:20 org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 587 ms
26/08/2012 18:33:20 org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
26/08/2012 18:33:20 org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.29
26/08/2012 18:33:23 org.jboss.weld.bootstrap.WeldBootstrap <clinit>
INFO: WELD-000900 1.1.8 (Final)
26/08/2012 18:33:23 org.jboss.weld.bootstrap.WeldBootstrap startContainer
INFO: WELD-000101 Transactional services not available. Injection of @Inject UserTransaction not available. Transactional observers will be invoked synchronously.
26/08/2012 18:33:23 org.jboss.weld.environment.tomcat7.Tomcat7Container initialize
INFO: Tomcat 7 detected, CDI injection will be available in Servlets and Filters. Injection into Listeners is not supported
26/08/2012 18:33:23 org.jboss.weld.interceptor.util.InterceptionTypeRegistry <clinit>
AVISO: Class 'javax.ejb.PostActivate' not found, interception based on it is not enabled
26/08/2012 18:33:23 org.jboss.weld.interceptor.util.InterceptionTypeRegistry <clinit>
AVISO: Class 'javax.ejb.PrePassivate' not found, interception based on it is not enabled
26/08/2012 18:33:23 com.sun.faces.config.ConfigureListener contextInitialized
INFO: Inicializando Mojarra 2.1.12 ( 20120814-1522) para o contexto '/sistema-transportadora'
26/08/2012 18:33:24 com.sun.faces.spi.InjectionProviderFactory createInstance
INFO: JSF1048: Anotações PostConstruct/PreDestroy presentes. Os métodos ManagedBeans marcados com essas anotações informarão as anotações processadas.
26/08/2012 18:33:24 org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
26/08/2012 18:33:24 org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
26/08/2012 18:33:24 org.apache.catalina.startup.Catalina start
INFO: Server startup in 4488 ms
Entrou aqui!!!
26/08/2012 18:33:28 com.sun.faces.lifecycle.InvokeApplicationPhase execute
AVISO: #{usuarioMB.listarTeste()}: java.lang.NullPointerException
javax.faces.FacesException: #{usuarioMB.listarTeste()}: java.lang.NullPointerException
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:117)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1001)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
Caused by: javax.faces.el.EvaluationException: java.lang.NullPointerException
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:101)
... 23 more
Caused by: java.lang.NullPointerException
at br.com.transportadora.web.managedbeans.UsuarioLoginMB.listarTeste(UsuarioLoginMB.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.el.parser.AstValue.invoke(AstValue.java:264)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)
at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:39)
at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
... 24 more
26/08/2012 18:33:28 org.apache.catalina.core.StandardWrapperValve invoke
GRAVE: Servlet.service() for servlet [Faces Servlet] in context with path [/sistema-transportadora] threw exception [java.lang.NullPointerException] with root cause
java.lang.NullPointerException
at br.com.transportadora.web.managedbeans.UsuarioLoginMB.listarTeste(UsuarioLoginMB.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.el.parser.AstValue.invoke(AstValue.java:264)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)
at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:39)
at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:101)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1001)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
O que pode ser este erro…em tese não era para o CDI injetar?
Não esta injetando?
Alguém pode me ajudar?
Obrigado.