AfterReturning注释不适用于特定的方法结构 [英] AfterReturning annotation not working for specific method structure

查看:629
本文介绍了AfterReturning注释不适用于特定的方法结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用AspectJ的@AfterReturning获得特定函数调用的返回值.

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

不确定为什么@AfterReturning在以下方法调用中不起作用.

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

尽管我试图在同一个类的2个方法上使用@AfterReturning,但是1个在其他方法不起作用时起作用. 两种方法之间唯一的区别是参数数量,其中@AfterReturning适用于带有一个参数的方法.

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.

工作

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

不起作用

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

通用规范也不起作用(用于2个方法参数)

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

存在2种方法的服务类

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?

推荐答案

这是经典之作:您在错误的位置寻找答案.不是切入点是问题,您的应用程序类与Spring AOP的基于代理的性质相结合的是:

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:

正如Spring手册在了解AOP代理,Spring AOP不能用于自调用.

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

您的method2是直接从method1而不是从外部调用的,因此方法调用转到原始对象,而不是AOP代理.因此,任何方面都不会在那里触发.

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.

这篇关于AfterReturning注释不适用于特定的方法结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆