创建方面从方法检索对象并返回另一个对象

时间:2013-08-14 08:09:04

标签: java spring aop spring-aop

我正在尝试编写一个方面(在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?


    }

谢谢, 射线。

1 个答案:

答案 0 :(得分: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