Wua*_*ner 6 java annotations intellij-idea interceptor cdi
似乎有两种方法将拦截器绑定到目标类/方法:
interceptor binding type(也就是用@InterceptorBinding本身注释的自定义注释,例如@Logged),并在目标类/方法上使用它我在CDI环境中使用拦截器.我的问题是,interceptor binding type如果我使用@Interceptors将拦截器绑定到我的目标方法,是否完全没有必要声明一个额外的?
如果答案是肯定的,那么为什么IntelliJ IDEA经常向我提出错误
@Interceptor必须至少指定一个拦截器绑定
当我没有interceptor binding type在拦截器上注释时?
如果答案是否定的,我已经将拦截器与@Interceptors(arrayOfMyInceptor)直接绑定到目标类/方法,为什么要声明一个额外的interceptor binding type并在我的拦截器上使用它?
我在网上搜索但是找不到这两种方法的差异,希望SO可以解决我的问题.
感谢您的耐心等待.
注释@Interceptor和其他服装注释@Logged应该在拦截器类上调用,例如
@Logged
@Interceptor
@Priority(Interceptor.Priority.APPLICATION)
public class LoggedInterceptor implements Serializable { ... }
Run Code Online (Sandbox Code Playgroud)
必须在要创建的注释上调用该注释@InterceptorBinding,以明确它在某种程度上是“拦截器限定符”。
@Inherited
@InterceptorBinding
@Retention(RUNTIME)
@Target({METHOD, TYPE})
public @interface Logged {
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以在(托管)bean 或其方法上调用拦截器绑定注释。
@Logged
public String pay() {...}
@Logged
public void reset() {...}
Run Code Online (Sandbox Code Playgroud)
请参阅java教程以获取更多帮助https://docs.oracle.com/javaee/7/tutorial/cdi-adv006.htm
编辑
因为我误解了你的问题,所以这是我的编辑:注释@Interceptors就像拦截器的集合。通过将几个拦截器类(例如LoggedInterceptorfrom @Logged)传递给value所应用@Interceptors注释的变量,所有这些拦截器绑定都会被调用:
@Interceptors({LoggedInterceptor.class,
OtherInterceptor.class,.....})
Run Code Online (Sandbox Code Playgroud)
因此,您至少需要一个拦截器绑定@Interceptors
因此,您需要拦截器类本身的拦截器绑定,但不需要目标类的拦截器绑定,因为 @Interceptors 注释中已经提到了它。
请参阅 API 文档https://docs.oracle.com/javaee/7/api/javax/interceptor/Interceptors.html
| 归档时间: |
|
| 查看次数: |
2946 次 |
| 最近记录: |