JEE6:在拦截器实现中获取 Binding 注释
我有一个拦截器绑定,它是参数化的:
@InterceptorBinding
@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface Traced {
@Nonbinding
boolean traceFull() default true;
}
然后我定义一个拦截器实现
@Interceptor
@Traced
public class TracingInterceptor implements Serializable { ... }
在实现中我想检查哪个值设置为traceFull参数。 (我不想实现 true、false 和 null 的三个拦截器实现)
因此,我的实现会检查被拦截方法的拦截器绑定注释:
Traced traceAnnotation = context.getMethod().getAnnotation(Traced.class);
if (traceAnnotation != null && traceAnnotation.traceFull()) { ... }
这很好用,但如果我使用 Stereotype 或嵌套拦截器绑定,那么我不会t 获取该方法的 @Traced annotatopn,我无法检查设置的值。
所以我的问题是:如何在拦截器实现中获得“调用”绑定?
I have an interceptor binding, which is parameterized:
@InterceptorBinding
@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface Traced {
@Nonbinding
boolean traceFull() default true;
}
Then I define an interceptor implementation
@Interceptor
@Traced
public class TracingInterceptor implements Serializable { ... }
In the implementation I want to check which value is set to the traceFull-parameter.
(I do not want to implement three interceptor implementations for true, false and null)
So my implementation checks the Interceptor-Binding-Annotation of the intercepted method:
Traced traceAnnotation = context.getMethod().getAnnotation(Traced.class);
if (traceAnnotation != null && traceAnnotation.traceFull()) { ... }
This works fine but if I use a Stereotype or a nested interceptor binding then I don't get the @Traced annotatopn of the method and I cannot check the value which is set.
So my question is: How can I get the 'calling' binding within my interceptor implementation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您甚至可以使用反射来解析构造型中的注释。
在同样的情况下(需要拦截器中的附加非绑定信息),我实现了一个实用方法:
You can use reflection even for resolving the annotation from stereotypes.
Being in the same situation (requiring additional nonbinding information in the interceptor), I have implemented a utility method:
这在 CDI 1.0 中不可能,但在 CDI 1.1 中应该可以
This is not possible in CDI 1.0, but should be in CDI 1.1