Guice post 执行方法拦截
Guice post execution method interception
在 Guice 中,有没有一种方法可以让我的 MethodInterceptor::invoke
实现被调用 在 被拦截的方法被执行之后(而不是紧接在之前)?
我已将当前代码添加到我的 AbstractModule
:
bindInterceptor(Matchers.subclassesOf(InterceptedClass.class), Matchers.annotatedWith(MyMethodAnnotation.class), new MyMethodInterceptor());
要在拦截器中调用方法后执行代码(这不仅适用于 Guice),您必须使用 try/finally 组合:
public Object invoke(MethodInvocation invocation) throws Throwable {
try {
// code run before execution
return invocation.proceed();
} finally {
// code run after execution
}
}
在 Guice 中,有没有一种方法可以让我的 MethodInterceptor::invoke
实现被调用 在 被拦截的方法被执行之后(而不是紧接在之前)?
我已将当前代码添加到我的 AbstractModule
:
bindInterceptor(Matchers.subclassesOf(InterceptedClass.class), Matchers.annotatedWith(MyMethodAnnotation.class), new MyMethodInterceptor());
要在拦截器中调用方法后执行代码(这不仅适用于 Guice),您必须使用 try/finally 组合:
public Object invoke(MethodInvocation invocation) throws Throwable {
try {
// code run before execution
return invocation.proceed();
} finally {
// code run after execution
}
}