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
}
}