首页 > 解决方案 > 为什么永远不会调用@AfterReturning

问题描述

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

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 建议”图标。我错过了什么?

这是我的课的样子:

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

标签: javaspring-aop

解决方案


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

这是类的样子:

@Service
@RequiredArgsConstructor
public class ReconciliationService {

    private final ReconciliationRepository reconciliationRepository;

    private final ReportSafeStoreClientService reportSafeStoreClientService;

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

}

你可以在这里找到更多信息


推荐阅读