使用 Spring Boot Aspect @After Advice 获取内部方法结果变量数据

问题描述 投票:0回答:1

我想要捕获我想要执行建议的方法内部的方法返回的数据。 我想使用@After,因为这个方法可以抛出异常,并且无论“父”方法是成功还是失败并以异常终止,我都想捕获这些数据。

我尝试了切入点的各种实现,但我不知道如何设置@After注释签名。 我读过的 Spring 文档/其他示例没有像我所要求的“高级”示例。 它们更“基本”。

基本上是这样的:AspectJ:拦截另一个方法中方法的返回结果

但是使用@After,而不是@AfterReturning。 @After 不支持与 @AfterReturning 相同的参数。

任何例子都很棒,谢谢。

应用类代码:

  package mycode.application;

  import class.from.external.jar.ExternalClass;


 public class MyClass{
  @Autowired
  ExternalClass ec;
 
  public ResponseObj method1(RequestObj reqObj) throws Exception {
   Object obj;
   ResponseObj retObj;
    // some code
    obj = ec.someMethod(reqObj);
    // some code
    try{}catch(Exception e{};
   return retObj;
  }
}

方面类代码:

package mycode.aspect;

import java.util.List;

import * (all the stuff we need to import)

@Aspect
public class MyAspect {

  @After("execution (* mycode.application.MyClass(..)")
  public void afterMethod1(JoinPoint joinPoint) {

      //the execution signature works great to get in here, but I can only get 
      // args/return object from the joinPoint.
      //I want to use the data from obj returned from called method inside method1
      //obj = ec.someMethod(..)

    );
  }
}
java spring spring-boot aop aspect
1个回答
0
投票

您必须使用

@AfterReturning
@AfterThrowing

或者只是

@Around
并自己捕获异常。

© www.soinside.com 2019 - 2024. All rights reserved.