Spring AOP :对于具有@Around方面的接口,参数作为空传递



我有一个问题,我在两个配置为 Spring bean 的接口中使用@Around。其中一个接口是另一个接口的参数,并且始终作为 null 值传递。以下是代码片段

      public interface Interface1 {
          public void method1();
      }
      public interface Interface2 {
          public void method2(Interface1 param1);
      }
      @Around("execution(* Interface1.method1(..))")
      private void interceptor1(ProceedingJoinPoint pJoinPoint) throws Throwable{
        //do something
      }
       @Around("execution(* Interface2.method2(..))")
      private void interceptor2(ProceedingJoinPoint pJoinPoint) throws Throwable{
        //do something
      }

在对接口 2 的调用代码中,我总是将参数 param1 作为 null 获取方法2。如果我删除上面的@Around("执行(* Interface1.method1(..))"),它可以正常工作。为它们添加@Around的原因是捕获用于日志记录和审核目的的异常,并停止传播其余异常。

你能帮我解决这个问题吗?

看起来你的方面是有缺陷的。环绕方面应始终具有返回类型Object而不是void。返回 void 基本上破坏了从调用堆栈正确传递返回值,请记住,around 方面将代码放在方法执行周围

因此,更改您的方面以返回对象,并始终返回调用的结果proceed()

public Object aroundAdvice(ProceedingJoinPoint pjp) {
  // Your stuff to do before the method call here
  Object returnValue = pjp.proceed();
  // Your stuff to do after the method call here
  return returnValue;
}

最新更新