【问题标题】:JEE6: Get the Binding annotation in an interceptor implementationJEE6:在拦截器实现中获取绑定注解
【发布时间】:2012-02-17 22:41:38
【问题描述】:

我有一个拦截器绑定,它是参数化的:

@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 参数。 (我不想实现真假空三种拦截器实现)

所以我的实现会检查被拦截方法的Interceptor-Binding-Annotation:

Traced traceAnnotation = context.getMethod().getAnnotation(Traced.class);
if (traceAnnotation != null && traceAnnotation.traceFull()) { ... }

这很好用,但如果我使用 Stereotype 或嵌套拦截器绑定,那么我不会得到该方法的 @Traced annotatopn 并且我无法检查设置的值。

所以我的问题是:如何在我的拦截器实现中获得“调用”绑定?

【问题讨论】:

    标签: jakarta-ee cdi interceptor


    【解决方案1】:

    您甚至可以使用反射来解析原型中的注释。

    在同样的情况下(在拦截器中需要额外的非绑定信息),我实现了一个实用方法:

    public <T extends Annotation> T findBindingAnnotation( Class<T> bindingType, InvocationContext ic )
    {
        Method method = ic.getMethod();
        if ( method != null && method.isAnnotationPresent( bindingType ) )
        {
            return method.getAnnotation( bindingType );
        }
        Class<? extends Object> type = ic.getTarget().getClass();
        if ( type.isAnnotationPresent( bindingType ) )
        {
            return type.getAnnotation( bindingType );
        }
    
        T annotationFromStereoType = annotationFromStereoType( bindingType, type );
        if ( annotationFromStereoType != null )
        {
            return annotationFromStereoType;
        }
    
        throw new UnsupportedOperationException( "no binding annotation found: " + bindingType.getCanonicalName() );
    }
    
    private <T extends Annotation> T annotationFromStereoType( Class<T> bindingType, Class<? extends Object> type )
    {
        for ( Annotation annotation : type.getAnnotations() )
        {
            Class<? extends Annotation> annotationType = annotation.annotationType();
            if ( annotationType.isAnnotationPresent( Stereotype.class ) )
            {
                if ( annotationType.isAnnotationPresent( bindingType ) )
                {
                    return annotationType.getAnnotation( bindingType );
                }
                else
                {
                    T recursiveLookup = annotationFromStereoType( bindingType, annotationType );
                    if ( null != recursiveLookup )
                    {
                        return recursiveLookup;
                    }
                }
            }
        }
        return null;
    }
    

    【讨论】:

      【解决方案2】:

      这在 CDI 1.0 中是不可能的,但应该在 CDI 1.1 中

      【讨论】:

      • 非常感谢您的回答。但是有什么方法可以通过注释使拦截器参数化?也许我必须用 Beanmanagers 或类似的东西编写一些代码?
      • 您也许可以通过 java 反射从 innvocationcontext 获取注释。 IIRC,其中很多都受到拦截器规范的限制。
      • 我正在使用反射来获取它,在调用上下文中使用 getMethod() 和 getTarget() 方法。问题是 WELD 正在返回 getTarget() 的包装实例,而不是实例本身。如果我的注解是@Inherited,则问题不大,但如果不是,则很痛苦。遗憾的是,我还没有让它适用于 Stereotypes,所以不要使用它们。
      猜你喜欢
      • 2013-09-21
      • 2014-10-03
      • 2011-07-03
      • 1970-01-01
      • 1970-01-01
      • 2019-04-27
      • 1970-01-01
      • 1970-01-01
      • 2012-04-06
      相关资源
      最近更新 更多