使用拦截器

时间:2018-03-18 08:21:06

标签: java interception

我是第一次使用拦截器。我按照A. Goncalves的过程,我尝试实现一个简单的记录器拦截器如下,但我得到了一个N​​PE。我不明白什么是错的,请向我解释。 我使用了限定符。

限定符实施

package classes;

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import java.lang.annotation.Retention;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Target;
import javax.interceptor.InterceptorBinding;

@InterceptorBinding
@Retention(RUNTIME)
@Target({METHOD,TYPE})
public @interface Loggable {

}

拦截器实施

package classes;

import java.util.logging.Logger;
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;

@Loggable
@Interceptor
public class LoggingInterceptor {

    private Logger logger;

    @AroundInvoke
    private Object intercept(InvocationContext ic) throws Exception{
        logger.info("> Entry");
        return ic.proceed();
    }
}

控制器实施

package classes;

import java.io.Serializable;
import javax.faces.bean.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;

@Loggable
@ViewScoped
@Named
public class BookService implements Serializable{

    public BookService (){
    }

    @Inject
    @ThirteenDigits
    private NumberGenerator generator;

    @Loggable
    public void somePrintMethod(){
        System.err.println("Hello world");
    }
}

XHTML页面实施

<?xml version="1.0" encoding="UTF-8"?>
<!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:p="http://primefaces.org/ui"
      >
    <h:head>
        <title>Interceptor</title>
    </h:head>
    <h:body>
        <h:form>
            <p:commandButton value="Create" action="#{bookService.somePrintMethod}"/>
        </h:form>
    </h:body>
</html>

错误

  

Avertissement:#{bookService.somePrintMethod}:   java.lang.NullPointerException javax.faces.FacesException:

     

{bookService.somePrintMethod}:com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:118)中的java.lang.NullPointerException

     

在javax.faces.component.UICommand.broadcast(UICommand.java:315)at at   javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)     在   javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)     在   com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)     在com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)at   com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)     在javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)at   org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)     在   org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)     在   org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)     在   org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)     在   org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)     在   org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)     在   org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)     在   org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)     在   com.sun.enterprise.v3.services.impl.ContainerMapper $ AdapterCallable.call(ContainerMapper.java:317)     在   com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)     在   com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)     在   com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)     在   com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)     在   com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)     在   com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)     在   com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)     在   com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)     在   com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)     在   com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)     在   com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)     在com.sun.grizzly.ContextTask.run(ContextTask.java:71)at   com.sun.grizzly.util.AbstractThreadPool $ Worker.doWork(AbstractThreadPool.java:532)     在   com.sun.grizzly.util.AbstractThreadPool $ Worker.run(AbstractThreadPool.java:513)     在java.lang.Thread.run(Thread.java:745)引起:   javax.faces.el.E​​valuationException:java.lang.NullPointerException at   javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)     在   com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)     ... 31更多引起:java.lang.NullPointerException at   classes.LoggingInterceptor.intercept(LoggingInterceptor.java:25)at   sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:606)at   org.jboss.weld.interceptor.proxy.SimpleMethodInvocation.invoke(SimpleMethodInvocation.java:30)     在   org.jboss.weld.interceptor.proxy.SimpleInterceptionChain.invokeNextInterceptor(SimpleInterceptionChain.java:68)     在   org.jboss.weld.interceptor.proxy.InterceptorMethodHandler.executeInterception(InterceptorMethodHandler.java:112)     在   org.jboss.weld.interceptor.proxy.InterceptorMethodHandler.invoke(InterceptorMethodHandler.java:88)     在   org.jboss.weld.bean.proxy.CombinedInterceptorAndDecoratorStackMethodHandler.invoke(CombinedInterceptorAndDecoratorStackMethodHandler.java:53)     在   classes.BookService $ Proxy $ _ $$ WeldSubclass.somePrintMethod(BookService $ Proxy $ $$ _ WeldSubclass.java)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:606)at   com.sun.el.parser.AstValue.invoke(AstValue.java:254)at at   com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:302)     在   org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:39)     在   org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)     在   com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)     在   javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)     ......还有32个

0 个答案:

没有答案