开发者

Using EJB interceptors after a method call

I know one can use interceptors befor开发者_JAVA技巧e a method call by using the @AroundInvoke annotation.

What I would like to do is execute certain code after the method call, so that I can for example create a log entry before and after a method execution.

Is this possible with EJB3, or do I need to use AOP?


@AroundInvoke interceptor is passed InvocationContext, and proceed() must be called to advance the method. Thus:

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

Depending on your needs, you could also log the return value or exceptions, filter the methods being logged, etc.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜