0

I am trying to use @AfterReturning of AspectJ to get return value of a specific function call.

Not sure why @AfterReturning not working for the following method call.

Though I am trying to use @AfterReturning on 2 methods of same class, 1 works when another didn't. Only difference between two methods are number of arguments, where @AfterReturning working for method with one argument.

Working

@AfterReturning(
  pointcut = "execution(org.springframework.http.ResponseEntity com.service.QueryGenerationService.method1(*))",
  returning = "retVal"
)
public void interceptMethod1(ResponseEntity retVal) {
  System.out.println(retVal+"---->");
}

Not working

@AfterReturning(
  pointcut = "execution(com.entity.ReportGenerationExportResult com.service.QueryGenerationService.method2(com.entity.ReportGenerationServiceRequest, com.entity.querybuilder.QueryBuilderResponse))",
  returning = "retVal"
)
public void interceptMethod2(ReportGenerationExportResult retVal) {
  System.out.println(retVal);
}

Generic specification also not working(for 2 method parameters)

@AfterReturning(
  pointcut = "execution(* com.service.QueryGenerationService.method2(*, *))",
  returning = "retVal"
)
public void test1(Object retVal){
  System.out.println(retVal);
}

Service class where 2 methods exist

@Service
public class QueryGenerationService {

  public ResponseEntity method1(
    ReportGenerationServiceRequest request
  ) throws Exception
  {
    //some logic
    ReportGenerationExportResult exportResult = method2(request, queryBuilderResponse);
    return toResponseEntity(exportResult);
  }

  public ReportGenerationExportResult method2(
    ReportGenerationServiceRequest originalRequest,
    QueryBuilderResponse queryBuilderResponse
  ) throws Exception
  {
    //some logic
    return reportGenerationExportResult;
  }
}

How can I successfully get the return value of second method?

kriegaex
  • 63,017
  • 15
  • 111
  • 202
Codex
  • 1,153
  • 1
  • 20
  • 31
  • Please share code for the class `com.service.QueryGenerationService` as well.. – DhaRmvEEr siNgh Aug 09 '19 at 09:43
  • You declared pointcut with two parameters (in all examples that's not working as you said), but your `method1` only has one parameter. Have you tried setting correct number of parameters? – M. Prokhorov Aug 09 '19 at 10:01
  • Please have a look, for method1(working expression) I have only one parameter. Just defined a return type. – Codex Aug 09 '19 at 10:34

2 Answers2

4

This one is a classic: You are looking for the answer in the wrong place. Not the pointcut is the problem, your application class in combination with Spring AOP's proxy-based nature is:

As the Spring manual clearly explains in chapter Understanding AOP Proxies, Spring AOP does not work for self-invocation.

Your method2 is called directly from method1, not from outside, thus the method call goes to the original object, not to the AOP proxy. Consequently, no aspect will fire there.

If you need aspects working with self-invocation you need to switch from Spring AOP to full-featured ASpectJ as described here.

kriegaex
  • 63,017
  • 15
  • 111
  • 202
0

issue is in your regular expression.. I think this link can help you..

Your code for both the methods should be like...

 //interceptMethod2

@AfterReturning(pointcut = "execution(*   com.service.QueryGenerationService.method1(com.entity.ReportGenerationServiceRequest))", returning = "retVal")
public void interceptMethod2(ReportGenerationExportResult retVal) {
    System.out.println(retVal);
}


//generic method which execute for all the methods in class QueryGenerationService
@AfterReturning(
        pointcut = "execution(* com.service.QueryGenerationService.*(..))"
        , returning = "retVal")
public void test1(Object retVal){
    System.out.println(retVal);
}
DhaRmvEEr siNgh
  • 1,918
  • 2
  • 13
  • 17
  • Could you please mention syntax for method2(which has 2 parameters)? That is the method that is not getting called by @AfterReturning. Thank you. – Codex Aug 09 '19 at 10:29
  • @AfterReturning( pointcut = "execution(com.entity.ReportGenerationExportResult com.service.QueryGenerationService.method2(com.entity.ReportGenerationServiceRequest, com.entity.querybuilder.QueryBuilderResponse))" , returning = "retVal") you can use this point cut to match your method 2.. – DhaRmvEEr siNgh Aug 09 '19 at 10:43
  • Please look into the code. I tried the same. It is mentioned in 'Not working' section. I wonder whats wrong with this code. – Codex Aug 09 '19 at 10:51
  • You have use "com.service.QueryGenerationService.method1" call instead of "com.service.QueryGenerationService.method2".. :) – DhaRmvEEr siNgh Aug 09 '19 at 10:55
  • Thank you for correcting me, I edited the question. Still problem exists. I mean method1 which has 1 argument getting called but not methid2 which has 2 arguments. – Codex Aug 09 '19 at 10:59
  • This is not the correct answer. It would be better to check the code in the question more carefully before just guessing and by that confusing the OP. BTW, AspectJ pointcut syntax does not use "regular expressions" (RE), the syntax is different from RE - no offense. – kriegaex Aug 09 '19 at 11:50