在方法调用之后使用EJB拦截器 [英] Using EJB interceptors after a method call
问题描述
我知道可以在方法调用之前使用@AroundInvoke批注使用拦截器。
I know one can use interceptors before 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.
EJB3是否可以实现此功能,还是需要使用AOP?
Is this possible with EJB3, or do I need to use AOP?
推荐答案
@AroundInvoke拦截器已传递InvocationContext,必须调用proce()来推进该方法。因此:
@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.
这篇关于在方法调用之后使用EJB拦截器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!