In Guice, is there a way for my MethodInterceptor::invoke implementation to be invoked after the intercepted method is executed (and not immediately before)?
I've added the current code to my AbstractModule:
bindInterceptor(Matchers.subclassesOf(InterceptedClass.class), Matchers.annotatedWith(MyMethodAnnotation.class), new MyMethodInterceptor());
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
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With