AfterReturning annotation not working for specific method structure

风格不统一 提交于 2019-12-13 04:36:18

问题


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?


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/57427286/afterreturning-annotation-not-working-for-specific-method-structure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!