Java JEE6拦截器-提取方法变量
是否可以在方法变量被截取时提取一次?我不知道;我不想截取参数,但要截取方法中的属性值?e、 gJava JEE6拦截器-提取方法变量,java,java-ee-6,cdi,interceptor,jboss-weld,Java,Java Ee 6,Cdi,Interceptor,Jboss Weld,是否可以在方法变量被截取时提取一次?我不知道;我不想截取参数,但要截取方法中的属性值?e、 g Business Logic: @MyInterceptor void myMethod(Object o){ ArrayList myList= null; myList= dao.getRecords(o.getId) //intercept the result of this dao call //I only want to call doWork after I have '
Business Logic:
@MyInterceptor
void myMethod(Object o){
ArrayList myList= null;
myList= dao.getRecords(o.getId) //intercept the result of this dao call
//I only want to call doWork after I have 'validated' contents of myList in interceptor
doWork(myList)
}
The Interceptor:
@Interceptor
@MyInterceptor
MyInterceptor{
@AroundInvoke{
public Object invoke(InvocationContext ctx) throws Exception {
//retrieve the contents of myList above and perform validation
//if it passes validation call ctx.proceed else return error
}
}
谢谢恐怕你不能用拦截器真正做到这一点,因为它们无法访问方法内部变量(只需看看调用上下文)。因此,您唯一的机会是将
myList
设置为bean属性,然后在拦截器中执行此操作
@AroundInvoke{
public Object invoke(InvocationContext ctx) throws Exception {
if(ctx.getTarget() instanceof BeanWithListProperty) {
Object toProceed = ctx.proceed();
BeanWithListProperty bean = (BeanWithListProperty) ctx.getTarget();
List list = bean.getMyList();
return toProceed;
}
return ctx.proceed();
}
另一个选择是使用这将导致更可读和更高效的代码
然而,我不太喜欢这些解决方案,在我看来,您的代码设计得并不好,您想实现什么?谢谢,我考虑的另一个选择是将DAO移动到拦截器,但它有其他缺点,并不真正适用于所有用例。谢谢您的回复