Exercício 5.6 apostila Caelum fj28 DaoInterceptor

3 respostas
T

Olá comunidade java, sou iniciante com java web e estou tentando seguir a apostila fj-28 da caelum, mas quando chego no fim do exercício 5.8 que é pra criar o DaoInceptor da o seguinte erro:

HTTP Status 500 -

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Unable to instantiate using public lojavirtual.loja.DaoInterceptor()

org.vraptor.http.DefaultDispatcher.dispatch(DefaultDispatcher.java:55)

org.vraptor.VRaptorServlet.service(VRaptorServlet.java:48)

javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

root cause

org.vraptor.interceptor.InterceptorInstantiationException: Unable to instantiate using public lojavirtual.loja.DaoInterceptor()

org.vraptor.interceptor.InterceptorDealer.poll(InterceptorDealer.java:68)

org.vraptor.core.InterceptorsLogicFlow.execute(InterceptorsLogicFlow.java:58)

org.vraptor.interceptor.FlashScopeInterceptor.intercept(FlashScopeInterceptor.java:22)

org.vraptor.core.InterceptorsLogicFlow.execute(InterceptorsLogicFlow.java:72)

org.vraptor.interceptor.RegisterAttributesInteceptor.intercept(RegisterAttributesInteceptor.java:47)

org.vraptor.core.InterceptorsLogicFlow.execute(InterceptorsLogicFlow.java:72)

org.vraptor.core.VRaptorExecution.execute(VRaptorExecution.java:97)

org.vraptor.core.DefaultController.execute(DefaultController.java:46)

org.vraptor.http.DefaultDispatcher.dispatch(DefaultDispatcher.java:44)

org.vraptor.VRaptorServlet.service(VRaptorServlet.java:48)

javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

root cause

org.vraptor.component.ComponentInstantiationException: Unable to instantiate using public lojavirtual.loja.DaoInterceptor()

org.vraptor.component.ComponentConstructor.newInstance(ComponentConstructor.java:45)

org.vraptor.interceptor.InterceptorType.newInstance(InterceptorType.java:66)

org.vraptor.interceptor.InterceptorDealer.poll(InterceptorDealer.java:62)

org.vraptor.core.InterceptorsLogicFlow.execute(InterceptorsLogicFlow.java:58)

org.vraptor.interceptor.FlashScopeInterceptor.intercept(FlashScopeInterceptor.java:22)

org.vraptor.core.InterceptorsLogicFlow.execute(InterceptorsLogicFlow.java:72)

org.vraptor.interceptor.RegisterAttributesInteceptor.intercept(RegisterAttributesInteceptor.java:47)

org.vraptor.core.InterceptorsLogicFlow.execute(InterceptorsLogicFlow.java:72)

org.vraptor.core.VRaptorExecution.execute(VRaptorExecution.java:97)

org.vraptor.core.DefaultController.execute(DefaultController.java:46)

org.vraptor.http.DefaultDispatcher.dispatch(DefaultDispatcher.java:44)

org.vraptor.VRaptorServlet.service(VRaptorServlet.java:48)

javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

root cause

java.lang.reflect.InvocationTargetException

sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)

sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)

java.lang.reflect.Constructor.newInstance(Unknown Source)

org.vraptor.component.ComponentConstructor.newInstance(ComponentConstructor.java:43)

org.vraptor.interceptor.InterceptorType.newInstance(InterceptorType.java:66)

org.vraptor.interceptor.InterceptorDealer.poll(InterceptorDealer.java:62)

org.vraptor.core.InterceptorsLogicFlow.execute(InterceptorsLogicFlow.java:58)

org.vraptor.interceptor.FlashScopeInterceptor.intercept(FlashScopeInterceptor.java:22)

org.vraptor.core.InterceptorsLogicFlow.execute(InterceptorsLogicFlow.java:72)

org.vraptor.interceptor.RegisterAttributesInteceptor.intercept(RegisterAttributesInteceptor.java:47)

org.vraptor.core.InterceptorsLogicFlow.execute(InterceptorsLogicFlow.java:72)

org.vraptor.core.VRaptorExecution.execute(VRaptorExecution.java:97)

org.vraptor.core.DefaultController.execute(DefaultController.java:46)

org.vraptor.http.DefaultDispatcher.dispatch(DefaultDispatcher.java:44)

org.vraptor.VRaptorServlet.service(VRaptorServlet.java:48)

javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

root cause

java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder

org.slf4j.LoggerFactory.(LoggerFactory.java:60)

org.hibernate.cfg.Configuration.(Configuration.java:151)

lojavirtual.util.HibernateUtil.(HibernateUtil.java:14)

lojavirtual.dao.DaoFactory.(DaoFactory.java:15)

lojavirtual.loja.DaoInterceptor.(DaoInterceptor.java:13)

sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)

sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)

java.lang.reflect.Constructor.newInstance(Unknown Source)

org.vraptor.component.ComponentConstructor.newInstance(ComponentConstructor.java:43)

org.vraptor.interceptor.InterceptorType.newInstance(InterceptorType.java:66)

org.vraptor.interceptor.InterceptorDealer.poll(InterceptorDealer.java:62)

org.vraptor.core.InterceptorsLogicFlow.execute(InterceptorsLogicFlow.java:58)

org.vraptor.interceptor.FlashScopeInterceptor.intercept(FlashScopeInterceptor.java:22)

org.vraptor.core.InterceptorsLogicFlow.execute(InterceptorsLogicFlow.java:72)

org.vraptor.interceptor.RegisterAttributesInteceptor.intercept(RegisterAttributesInteceptor.java:47)

org.vraptor.core.InterceptorsLogicFlow.execute(InterceptorsLogicFlow.java:72)

org.vraptor.core.VRaptorExecution.execute(VRaptorExecution.java:97)

org.vraptor.core.DefaultController.execute(DefaultController.java:46)

org.vraptor.http.DefaultDispatcher.dispatch(DefaultDispatcher.java:44)

org.vraptor.VRaptorServlet.service(VRaptorServlet.java:48)

javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

root cause

java.lang.ClassNotFoundException: org.slf4j.impl.StaticLoggerBinder

org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1386)

org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1232)

java.lang.ClassLoader.loadClassInternal(Unknown Source)

org.slf4j.LoggerFactory.(LoggerFactory.java:60)

org.hibernate.cfg.Configuration.(Configuration.java:151)

lojavirtual.util.HibernateUtil.(HibernateUtil.java:14)

lojavirtual.dao.DaoFactory.(DaoFactory.java:15)

lojavirtual.loja.DaoInterceptor.(DaoInterceptor.java:13)

sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)

sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)

java.lang.reflect.Constructor.newInstance(Unknown Source)

org.vraptor.component.ComponentConstructor.newInstance(ComponentConstructor.java:43)

org.vraptor.interceptor.InterceptorType.newInstance(InterceptorType.java:66)

org.vraptor.interceptor.InterceptorDealer.poll(InterceptorDealer.java:62)

org.vraptor.core.InterceptorsLogicFlow.execute(InterceptorsLogicFlow.java:58)

org.vraptor.interceptor.FlashScopeInterceptor.intercept(FlashScopeInterceptor.java:22)

org.vraptor.core.InterceptorsLogicFlow.execute(InterceptorsLogicFlow.java:72)

org.vraptor.interceptor.RegisterAttributesInteceptor.intercept(RegisterAttributesInteceptor.java:47)

org.vraptor.core.InterceptorsLogicFlow.execute(InterceptorsLogicFlow.java:72)

org.vraptor.core.VRaptorExecution.execute(VRaptorExecution.java:97)

org.vraptor.core.DefaultController.execute(DefaultController.java:46)

org.vraptor.http.DefaultDispatcher.dispatch(DefaultDispatcher.java:44)

org.vraptor.VRaptorServlet.service(VRaptorServlet.java:48)

javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

se alguem puder me ajudar serei grato.
Vlw.

3 Respostas

sarkozy

brother, posta o exercicio ae pra eu ver se posso te ajudar…!

T

Aeh Sarkozy o exercicio é colocar o DaoInterceptor para interceptar e grava no banco:

  1. Crie o DaoInterceptor no pacote br.com.caelum.lojavirtual.loja:
    public class DaoInterceptor implements Interceptor {
    private final DaoFactory factory = new DaoFactory();
    Capítulo 1 - Como aprender Java - Página 36
    Caelum ? http://www.caelum.com.br - Web 2.0 com VRaptor, Hibernate e Ajax
    public void intercept(LogicFlow flow) throws LogicException, ViewException {
    // executa a lógica
    flow.execute();
    // se sobrou transação sem comitar, faz rollback
    if (factory.hasTransaction()) {
    factory.rollback();
    }
    factory.close();
    }
    @Out(key=“br.com.caelum.lojavirtual.dao.DaoFactory”)
    public DaoFactory getFactory() {
    return factory;
    }
    }
  2. Altere sua classe UsuarioLogic para ser interceptada pelo
    DaoInterceptor:
    a) Adicione a anotação @InterceptedBy no topo de sua classe
    @Component
    @InterceptedBy(DaoInterceptor.class)
    public class UsuarioLogic {
    b) Adicione um atributo DaoFactory na sua classe:
    private final DaoFactory daoFactory;
    c) Crie um construtor que recebe DaoFactory e inicializa o atributo
    public UsuarioLogic(DaoFactory daoFactory) {
    this.daoFactory = daoFactory;
    }
    d) Implemente o método adiciona de forma a adicionar usando a
    DaoFactory
    public void adiciona (Usuario usuario) {
    this.daoFactory.beginTransaction();
    this.daoFactory.getUsuarioDao().adiciona(usuario);
    this.daoFactory.commit();
    }
    No fim, sua UsuarioLogic deve estar assim:
    @Component
    @InterceptedBy(DaoInterceptor.class)
    public class UsuarioLogic {
    private final DaoFactory daoFactory;
    public UsuarioLogic(DaoFactory daoFactory) {
    Capítulo 1 - Como aprender Java - Página 37
    Caelum ? http://www.caelum.com.br - Web 2.0 com VRaptor, Hibernate e Ajax
    this.daoFactory = daoFactory;
    }
    public void adiciona (Usuario usuario) {
    this.daoFactory.beginTransaction();
    this.daoFactory.getUsuarioDao().adiciona(usuario);
    this.daoFactory.commit();
    }
    }
  3. Teste novamente seu sistema. Mas desta vez olhe no banco de
    dados para ver o usuário inserido.
    mysql -u root teste
    select * from Usuario;

[b]Tem uma coisa que não entendi quando antes disso ele diz que para configurar os interceptadores, tem que abrir cada uma das 4 logicas e adicionar o @Intercepted By nas classes, mas quais são essas classes?

Vlw.[/b]

sarkozy

puts kra…interceptor,BD…dessas areas não entendo…mas da uma olhada nesses links:

http://edocs.beasys.com/wle/rli/javadevp.htm

http://www.vraptor.com.br/forum/posts/list/706.page

talvez te dê uma luz…hehehe

flw

Criado 26 de julho de 2009
Ultima resposta 26 de jul. de 2009
Respostas 3
Participantes 2