AOP advanced usage-get method parameters and return values

AOP advanced usage-get method parameters and return values

When we usually use springAop, we often define a certain method as an entry point for pre, post or surround enhancement, but if we want to get the parameters of the entry point method and its return value, we need to do something Specific configuration.

The general aspect is as follows:

@Configuration
@Aspect
public class UserInfoAspect {
    @Pointcut("execution(* com.*.test(*))")
    public void test() {}

    @AfterReturning(value = "test()"")
    public void logMethodCall() throws Throwable {
        System.out.println("The post-entry is enhanced!");
    }
}

Get the parameters of the pointcut method:

@Configuration
@Aspect
public class UserInfoAspect {
    @Pointcut("execution(* com.*.test(*))")
    public void test() {}
    
   //Use the JoinPoint object to receive the parameters of the pointcut method
    @AfterReturning(value = "test()")
    public void logMethodCall(JoinPoint jp) throws Throwable {

        System.out.println("The post-entry is enhanced!");
        String name = jp.getSignature().getName();
        System.out.println(name);

        Object[] args = jp.getArgs();
        for (Object arg: args) {
            System.out.println("Parameter:" + arg);
        }
    }
 }

As you can see from the above, you can use the JoinPoint object to get the parameter value of the pointcut method.

How to get the return value:

@Configuration
@Aspect
public class UserInfoAspect {

    @Pointcut("execution(* com.*.test(*))")
    public void test() {}
    
   //Declare returning in the event notification type to get the return value
    @AfterReturning(value = "test()", returning="returnValue")
    public void logMethodCall(JoinPoint jp, Object returnValue) throws Throwable {

        System.out.println("The post-entry is enhanced!");
        String name = jp.getSignature().getName();
        System.out.println(name);

        Object[] args = jp.getArgs();
        for (Object arg: args) {
            System.out.println("Parameter:" + arg);
        }

        System.out.println("Method return value:" + returnValue);

    }
}

As you can see from the above, declare "returing=returnValue" in the type of event notification, and then introduce "Object returnValue" in the aspect method to get the return value.

If you want to have multiple entry points at the same time, you can use logical operators "&&", "||", etc., as shown below:

@Pointcut("execution(* com.*.(*))")
public void addUser() {}
   
@Pointcut("execution(* com.*.(*))")
public void updateUser() {}

@After(value = "addUser() || updateUser()", returning="returnValue")
public void pushAccountInfo(JoinPoint jp, Object returnValue){
   //Write the aspect logic here:
}

What needs attention here is the use of logical operators. What is confusing is the difference between "&&" and "|| " . If you want an aspect to be effective for multiple entry points at the same time, you should use "||" Instead of "&&".