开发者

Using EJB interceptors after a method call

开发者 https://www.devze.com 2023-03-14 05:24 出处:网络
I know one can use interceptors befor开发者_JAVA技巧e a method call by using the @AroundInvoke annotation.

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

精彩评论

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