简体   繁体   中英

Creating aspect the retrieves object from method and return another object

I am trying to write an aspect (in Spring) which taking the input params from a method in my package do some manipulation and return to that method a result.

Is that possible?

For example:

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?


    }

Thanks, ray.

If you want to change the return value, you will have to use an @Around advice.

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

}

The mechanism is described here: Spring Reference > AOP > Around Advice

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM