Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Guice post execution method interception

Tags:

aop

guice

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());
like image 281
nozik Avatar asked Jul 10 '26 05:07

nozik


1 Answers

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
   }
}
like image 150
Jan Galinski Avatar answered Jul 11 '26 19:07

Jan Galinski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!