java - 为什么 @AfterReturning 从未被调用

标签 java spring-aop

我有这个方法,它确实返回一个列表:

public List<ReportReconciliationEntry> getMissingReports(List<ReportReconciliationEntry> expectedReports,
                                                              List<GeneratedReportContent> generatedReports){
        ...
        return missingReports;
    }

但这个方法从未被调用:

    @AfterReturning(value = "execution(* com.XXX.YYY.ZZZ.service.ReconciliationService.getMissingReports(..)) && args(expectedReports,generatedReports)", argNames = "expectedReports,generatedReports,missingReports",  returning = "missingReports")
public void logReportReconciliationException(List<ReportReconciliationEntry> expectedReports, List<GeneratedReportContent> generatedReports, List<ReportReconciliationEntry> missingReports) {
                final String notApplicable = properties.getNotApplicable();
                ReportingAlertMarker marker = ReportingAlertMarker.builder()
                        .eventType(E90217)
                        .userIdentity(notApplicable)
                        .destinationIp(properties.getDestinationIp())
                        .destinationPort(properties.getDestinationPort())
                        .dataIdentity(notApplicable)
                        .resourceIdentity(notApplicable)
                        .responseCode(404)
                        .build();
                MDC.put(SYSTEM_COMPONENT, properties.getBpsReportGenerationService());
                System.out.println(missingReports);
                logWrapper.logError(marker, "SDGFHDZFHDFR!!");
            }

我用断点检查第一个方法的返回。它确实返回一个列表,但 @AfterReturning 永远不会被调用,尽管 IDE 显示“导航到 AOP 建议”图标。我错过了什么?

这就是我的类(class)的样子:

   @Component
    @Aspect
    @Slf4j
    public class ReportingAlertAspect {

        private final LogWrapper logWrapper;

        private final ReportingAlertProperties properties;

        public ReportingAlertAspect(final ReportingAlertProperties properties, final LogWrapper logWrapper) {
            this.logWrapper = logWrapper;
            this.properties = properties;
        }
....
}

我有另一个类,其中有一个函数,这个类工作正常:

        @Component
        @Aspect
        @Slf4j
        public class ReportingInfoAspect {

            private final LogWrapper logWrapper;

            private final ReportingAlertProperties properties;

      @AfterReturning(value = "execution(* com.xxx.yyy.zzz.qqq.ReconciliationService.reconcile(..)) && args(windowId)", argNames = "windowId,check",
                returning = "check")
        public void logSuccessfulReportReconciliation(ReconciliationEvent windowId, boolean check){
            String notApplicable = properties.getNotApplicable();
            MDC.put(SYSTEM_COMPONENT, properties.getBpsReportGenerationService());
            ReportingAlertMarker marker = ReportingAlertMarker.builder()
                    .eventType(E90293)
                    .userIdentity(notApplicable)
                    .destinationIp(properties.getDestinationIp())
                    .destinationPort(properties.getDestinationPort())
                    .dataIdentity(notApplicable)
                    .resourceIdentity(notApplicable)
                    .responseCode(200)
                    .build();
            if (check){
                logWrapper.logInfo(marker, "All reports for windowId {} were generated successfully", windowId.windowId);
            }
        }
  }

最佳答案

我发现了问题。 getMissingReports 方法是从同一类中的另一个方法调用的。这是自调用的情况,并且该方法从未通过代理调用。

这就是类的样子:

@Service
@RequiredArgsConstructor
public class ReconciliationService {

    private final ReconciliationRepository reconciliationRepository;

    private final ReportSafeStoreClientService reportSafeStoreClientService;

    @Handler
    public whatever whatever() {
      ...
      getMissingReports()
    }

}

您可以找到更多信息here

关于java - 为什么 @AfterReturning 从未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59319563/

相关文章:

java - 创建自定义 Web 应用程序上下文 [SPRING]

java - 清理后 Maven 不编译。 "cannot find symbol"

java - AspectJ 切入点表达式匹配任意位置的参数注释

java - AOP AfterReturning 函数返回两次

java - 传递函数的所有参数

java - Java 应用程序的 Web 界面

Java HttpClient 获取 url 中含有非法字符的响应

java - 未调用建议

java - 为什么Spring-proxy使用委托(delegate)模式而不是继承+super?

java - 我无法使用 Spring AOP 调用建议的方法