【问题标题】:@Interceptor Interface with enum Methods returns only default value带有枚举方法的@Interceptor 接口仅返回默认值
【发布时间】:2013-11-10 10:50:51
【问题描述】:

我有以下情况

拦截器接口

@Inherited
@InterceptorBinding
@Documented
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface IErrorHandlerInterceptor {
    Car tyre() default Car.FOUR;
}

枚举类

 public enum Car{
        FOUR,FIVE;
    }

EJB 无状态类

@ErrorHandlerInterceptor(tyre= Car.FIVE)
public List<?> getCarByName(String name) {
    ----------
    return List<?>;
}

beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation=" http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
   <interceptors>
        <class>de.festado.interceptor.impl.ErrorHandlerInterceptor</class>
    </interceptors>
</beans>

拦截器实现

    @IErrorHandlerInterceptor
    @Interceptor
    public class ErrorHandlerInterceptor {

@AroundInvoke
public Object intercept(InvocationContext context) throws Exception {
    IErrorHandlerInterceptor ei = getClass().getAnnotation(IErrorHandlerInterceptor.class);
    String tyresNumber= ei.tyre().toString();
    try {
        return context.proceed();

    } catch(Exception e) {
        ....
    } finally {
        .....
    }
    return null;
}

现在我的问题:

每当我调用String tyresNumber= ei.tyre().toString(); 时,它只会返回我在接口声明中设置的默认值。

我在这里做错了什么? 我是不是忘记了什么?

感谢您的帮助

【问题讨论】:

    标签: java jakarta-ee annotations cdi interceptor


    【解决方案1】:

    这个答案CDI interceptor does not work when annotation has parameter 对我有很大帮助

    IErrorHandlerInterceptor ei = getClass().getAnnotation(IErrorHandlerInterceptor.class);
    

    getClass().getAnnotation(...) 是错误的。正确的调用必须来自上下文

    IErrorHandlerInterceptorei = context.getMethod().getAnnotation(IErrorHandlerInterceptor.class);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-03
      • 1970-01-01
      • 2011-04-18
      • 1970-01-01
      • 2021-08-24
      • 2019-01-09
      • 2022-01-24
      相关资源
      最近更新 更多