How to enhance the input and output parameters of AOP, you will understand after reading

Preface

In many cases, we will need to perform some additional processing on the input or output of the method. At this time, using AOP can meet our needs. Because AOP is less intrusive, has a low degree of code coupling with business logic, and has good reusability, and high development efficiency.

Here are some excerpts about AOP

AOP means aspect-oriented programming. AOP is a technology that uses pre-compilation and runtime dynamic agents to dynamically add functions to programs without modifying the source code. AOP is the continuation of OOP, a hot spot in software development, and an important content in the Spring framework. AOP can be used to isolate various parts of business logic, thereby reducing the coupling between various parts of business logic and improving programs Reusability, while improving the efficiency of development.

premise

To use aop in a Spring Boot project, you need to add the following dependencies

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
 </dependency>

AOP enhanced entry and exit

If we want to enhance the input and output parameters, then we need to use Around surround enhancement.

Here is a brief introduction to the role of Surround surround

Around enhancement : you can weave the enhancement action before the target method is executed, or after the method is executed, you can determine when and how the target method is executed. The input parameters and return value of the target can be changed.

Looking at the effect of Surround surround enhancement, we can see that Surround surround enhancement is very suitable for us to deal with the input and output parameters of the target method.

Below I will demonstrate the basic usage of Surround Enhancing the input and output parameters, you can refer to the processing of your own

@Aspect //定义切面类
@Component
public class ControllerAspect {
    
    

    /**
     * 定义切点
     */
    @Pointcut("execution(* com.example.demo2.controller.*.*(..))")
    public void point(){
    
    
    }

    /**
     * 定义环绕增强
     */
    @Around("point()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
    
    
        //获取入参
        Object[] args = joinPoint.getArgs();
        //获取方法名
        System.out.println(joinPoint.getSignature().getName());
        //获取方法所在类的类名
        System.out.println(joinPoint.getSignature().getDeclaringType().getSimpleName());
        //对入参进行处理
        int i=0;
        if (ArrayUtils.isNotEmpty(args)){
    
    
            //入参为对象
            Params params=null;
            for (Object arg : args) {
    
    
                //String类型或者Integer等包装类,需要对入参数组进行重新赋值
                if (arg instanceof String){
    
    
                    args[i]="aop处理后的入参";
                }
                //对象类型的入参直接修改属性即可
                if (arg instanceof Params){
    
    
                    params= (Params) arg;
                    params.setName("aop处理后的入参对象");
                }
                i++;
            }
        }
        //传入入参执行方法
        Object response = joinPoint.proceed(args);
        //修改返回值
        if (response instanceof String){
    
    
            response="修改后的返回值";
        }
        //将返回值返回,方法的返回值是around的返回值,而不是原来的返回值
        return response;
    }
}

The following summarizes the general process of AOP modification method input and output parameters

Modify the input parameters
1. Obtain the original input parameters through ProceedingJoinPoint

2. Enhance the original input

3. Pass the input parameters into the proceed of ProceedingJoinPoint, the execution method, at this time the method is equivalent to using the enhanced input parameter execution method

Modify the parameters
1. Receive the return value of the proceed method, which is the initial return value of the method

2. Enhance the initial return value

3. Return the enhanced return value, the method surrounded by around, its output parameter is the return value of the around method, not necessarily the original return value of the method, this is the essence of how we can enhance the return value

Test category:

@Controller
@RestController
public class TestController {
    
    


    @PostMapping(value = "testAop")
    public String testAop(String userName, Params params)throws Exception{
    
    
        System.out.println("入参为:  "+userName);
        System.out.println("入参对象为: "+params);
        return "正常返回值";
    }
}

Output after the call:

testAop
TestController
入参为:  aop处理后的入参
入参对象为: Params(name=aop处理后的入参对象)
修改后的返回值

Through the results, we can see that AOP has processed the input and output parameters of the method

Guess you like

Origin blog.csdn.net/qq_36551991/article/details/110499055