在方法调用之后使用EJB拦截器

时间:2011-06-21 14:07:07

标签: ejb-3.0 interceptor

我知道可以在方法调用之前使用@AroundInvoke注释来使用拦截器。

我想要做的是在方法调用之后执行某些代码,以便我可以在方法执行之前和之后创建一个日志条目。

EJB3可以实现这一点,还是需要使用AOP?

1 个答案:

答案 0 :(得分:18)

@AroundInvoke拦截器被传递给InvocationContext,并且必须调用proceed()来推进该方法。因此:

@AroundInvoke
public Object log(InvocationContext ic) throws Exception {
  logEntry();
  try {
    return ic.proceed();
  } finally {
    logExit();
  }
}

根据您的需要,您还可以记录返回值或异常,过滤记录的方法等。