Guice后执行方法拦截 [英] Guice post execution method interception
本文介绍了Guice后执行方法拦截的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Guice中,是否有办法在执行被拦截的方法之后(而不是之前)在之后调用MethodInterceptor::invoke
实现?
In Guice, is there a way for my MethodInterceptor::invoke
implementation to be invoked after the intercepted method is executed (and not immediately before)?
我已将当前代码添加到我的AbstractModule
:
I've added the current code to my AbstractModule
:
bindInterceptor(Matchers.subclassesOf(InterceptedClass.class), Matchers.annotatedWith(MyMethodAnnotation.class), new MyMethodInterceptor());
推荐答案
要在拦截器中调用方法后执行代码(这不仅适用于Guice),必须使用try/finally组合:
To execute code after the method invocation in an interceptor (this applies not just to Guice), you have to use a try/finally combination:
public Object invoke(MethodInvocation invocation) throws Throwable {
try {
// code run before execution
return invocation.proceed();
} finally {
// code run after execution
}
}
这篇关于Guice后执行方法拦截的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文