gpt4 book ai didi

java - AfterReturning 注释不适用于特定方法结构

转载 作者:行者123 更新时间:2023-12-01 19:36:13 24 4
gpt4 key购买 nike

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

不确定为什么 @AfterReturning 不适用于以下方法调用。

虽然我尝试在同一个类的 2 个方法上使用 @AfterReturning,但其中一个方法可以工作,而另一个方法则不能。两种方法之间的唯一区别是参数数量,其中 @AfterReturning 适用于只有一个参数的方法。

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

如何成功获取第二个方法的返回值?

最佳答案

这是一个经典:你在错误的地方寻找答案。问题不是切入点,您的应用程序类与 Spring AOP 的基于代理的性质相结合是:

Spring 手册在 Understanding AOP Proxies 章节中清楚地解释了这一点,Spring AOP 不适用于自调用。

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

如果您需要使用自调用方面,您需要从 Spring AOP 切换到功能齐全的 ASpectJ,如 here 所述。 .

关于java - AfterReturning 注释不适用于特定方法结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57427286/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com