achdumeingute
achdumeingute

Reputation: 46

Getting inner methods result variable data using Spring Boot Aspect @After Advice

I am wanting to capture the data returned by a method inside of a method I am wanting to perform advice on. I want to use @After, because this method can throw exceptions, and I want to capture this data regardless of if the "parent" method is successful or fails and terminates with an exception.

I have tried various implementations of pointcuts, but I don't know how to set up the @After annotation signature. The Spring documentation/other examples I have read do not have an "advanced" example like what I am asking for. They are more "basic".

It is BASICALLY this: AspectJ: Intercept return result of method inside another method

But with @After, not @AfterReturning. @After does not support the same parameters as @AfterReturning.

Any examples would be fantastic, thanks.

Application Class Code:

  package mycode.application;

  import class.from.external.jar.ExternalClass;


 public class MyClass{
  @Autowired
  ExternalClass ec;
 
  public ResponseObj method1(RequestObj reqObj) throws Exception {
   Object obj;
   ResponseObj retObj;
    // some code
    obj = ec.someMethod(reqObj);
    // some code
    try{}catch(Exception e{};
   return retObj;
  }
}

Aspect Class Code:

package mycode.aspect;

import java.util.List;

import * (all the stuff we need to import)

@Aspect
public class MyAspect {

  @After("execution (* mycode.application.MyClass(..)")
  public void afterMethod1(JoinPoint joinPoint) {

      //the execution signature works great to get in here, but I can only get 
      // args/return object from the joinPoint.
      //I want to use the data from obj returned from called method inside method1
      //obj = ec.someMethod(..)

    );
  }
}

Upvotes: 0

Views: 25

Answers (1)

talex
talex

Reputation: 20544

You have to use @AfterReturning and @AfterThrowing.

Or just @Around and catch exception yourself.

Upvotes: 0

Related Questions