我有以下案例
拦截器接口
@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();
时,它只返回我在接口声明中设置的默认值。
我在这做错了什么? 我忘了什么吗?
感谢您的帮助
答案 0 :(得分:0)
这里的答案CDI interceptor does not work when annotation has parameter对我有很大帮助
IErrorHandlerInterceptor ei = getClass().getAnnotation(IErrorHandlerInterceptor.class);
getClass().getAnnotation(...)
错了。正确的调用必须来自上下文
IErrorHandlerInterceptorei = context.getMethod().getAnnotation(IErrorHandlerInterceptor.class);