0
@InterceptorBinding
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface RequiresPageReload {
}
public interface Page{
public static final String LOAD_STR = "load";
public void load();
}
@RequestScoped
public class PageImpl1 implements Page{
public void load(){
//...
}
@RequiresPageReload
public String foo(){
//...
return "foo1";
}
}
@RequestScoped
public class MyObject{
@RequiresPageReload
public String foo2(){
//...
return "foo2";
}
}
@RequiresPageReload
@Interceptor
public class RequiresPageReloadInterceptor implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@AroundInvoke
public Object forceReload(InvocationContext context) throws Exception {
Object result = context.proceed();
context.getMethod().getDeclaringClass().getDeclaredMethod(Page.LOAD_STR).invoke(context.getTarget()); //***
return result;
}
}
在标有星号的行中,我当然可以通过反射检查方法是否存在,并据此决定做什么。 但我想知道是否有更好的方法来实现相同的行为? 例如,是否可以将拦截器关联到特定的类型(在本例中,假设我不希望截断MyObject的foo2()方法,因为此类对象不实现Page)?我考虑也使用装饰,但问题是“富”的方法不属于一个接口..特定类型的CDI拦截器
谢谢!
嗨!谢谢你的回答..正如我写的“我也考虑过使用装饰器,但问题在于”foo的方法不属于接口..“..我做了不属于接口的方法的注解,所以装饰者不会帮助.. :( – Federico 2012-07-25 06:32:57
在CDI中没有任何东西能够真正做到这一点,尽管你可以将它作为扩展名来做。 – LightGuard 2012-07-25 16:20:44