2

我正在尝试编写一个方面(在 Spring 中),它从我的包中的方法获取输入参数进行一些操作并将结果返回到该方法。

那可能吗?

例如:

public MyClass {

 Public void execute (Object object)
  {
     //doSomeLogic with the returned object from the aspect
  }
}

@Aspect
public class ExecutionAspect {




@Before(// any idea?)
        public void getArgument(JoinPoint joinPoint) {


         Object[] signatureArgs = joinPoint.getArgs();
         for (Object signatureArg: signatureArgs) {
             MyObject myObject=(MyObject)signatureArg;
             //do some manipulation on myObject
}
                  //Now how do I return the object to the intercepted method?


    }

谢谢,雷。

4

1 回答 1

10

如果要更改返回值,则必须使用@Around建议。

@Aspect
public class AroundExample {

  @Around("some.pointcut()")
  public Object doSomeStuff(ProceedingJoinPoint pjp) throws Throwable {

    Object[] args = joinPoint.getArgs(); // change the args if you want to
    Object retVal = pjp.proceed(args); // run the actual method (or don't)
    return retVal; // return the return value (or something else)
  }

}

该机制在此处描述:Spring Reference > AOP > Around Advice

于 2013-08-14T09:07:45.717 回答