Java拦截器,对@InterceptorBinding感到困惑

时间:2015-06-25 19:37:42

标签: java annotations interceptor

我正在玩拦截器和注释,但我很困惑,我找不到一个广泛的教程。 其实我不明白这个之间的区别:

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

和此:

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

@InterceptorBinding做什么?为什么要使用它,为什么不呢?

我正在寻找一些关于注释和拦截器的例子,包括不同的用例,但我找到了关于非常基本的用法或一些含糊的代码(至少对我而言)的文档,没有任何解释,所以谢谢你,如果你能给我一些指针。

1 个答案:

答案 0 :(得分:0)

考虑拦截器声明:

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

@InterceptorBinding 只是用于将注释绑定到特定拦截器的指示器。

  

应用程序与拦截器一起定义一个或多个拦截器绑定类型,这些类型是将拦截器与目标bean或方法相关联的注释。例如,billpayment示例包含一个名为@Logged的拦截器绑定类型和一个名为LoggedInterceptor的拦截器。

@Inherited 用于按类层次结构允许此注释。

  

拦截器绑定还具有java.lang.annotation.Inherited注释,以指定注释可以从超类继承。 @Inherited注释也适用于自定义范围(本教程中未讨论),但不适用于限定符。

另请查看documentation