参数拦截器在Java EE中不起作用

时间:2014-08-06 11:18:05

标签: java-ee parameters jboss cdi interceptor

在Java EE中,我试图在方法参数具有特定注释时调用拦截器。这是我的代码:

注释代码

@Retention(RUNTIME)
@Target(PARAMETER)
public @interface Token{
   //NOP
}

拦截器代码

@Interceptor
public class TokenInterceptor{

   @AroundInvoke
   public Object checkInvocation(InvocationContext ctx) throws Exception {
       //Actual Code that detects the presence of the annotation
   }

}

带注释参数的方法

public void processOrders(@Token List<Order> token) {}

的beans.xml

<interceptors>
    <class>com.project.security.TokenInterceptor</class>
</interceptors>

当我尝试部署JBoss服务器时,会出现以下错误。

  

引起:org.jboss.weld.exceptions.DeploymentException:WELD-000069拦截器必须至少有一个绑定,但是com.project.security.TokenInterceptor没有       在org.jboss.weld.bean.InterceptorImpl。(InterceptorImpl.java:72)       在org.jboss.weld.bean.InterceptorImpl.of(InterceptorImpl.java:59)       在org.jboss.weld.bootstrap.AbstractBeanDeployer.createInterceptor(AbstractBeanDeployer.java:229)       在org.jboss.weld.bootstrap.BeanDeployer.createBeans(BeanDeployer.java:149)       在org.jboss.weld.bootstrap.BeanDeployment.createBeans(BeanDeployment.java:204)       在org.jboss.weld.bootstrap.WeldBootstrap.deployBeans(WeldBootstrap.java:349)       在org.jboss.as.weld.WeldStartService.start(WeldStartService.java:63)       at org.jboss.msc.service.ServiceControllerImpl $ StartTask.startService(ServiceControllerImpl.java:1811)[jboss-msc-1.0.4.GA-redhat-1.jar:1.0.4.GA-redhat-1]       at org.jboss.msc.service.ServiceControllerImpl $ StartTask.run(ServiceControllerImpl.java:1746)[jboss-msc-1.0.4.GA-redhat-1.jar:1.0.4.GA-redhat-1]       ......还有3个

任何关于我缺少什么才能让它发挥作用的想法?

1 个答案:

答案 0 :(得分:3)

你需要像这样的每个拦截器都有绑定

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

拦截器本身需要用绑定注释

@Interceptor
@Logged
public void TokenInterceptor {

    @AroundInvoke
    public Object checkInvocation(InvocationContext ctx) throws Exception {
        //Actual Code that detects the presence of the annotation
    }
}  

现在,您可以使用@Logged注释

将拦截器绑定到任何方法或类
@Logged
public void processOrders(List<Order> token) {}

请参阅官方Java EE教程reference