• Aop is faceted programming and is commonly used in the following scenarios:

    • The transaction

    • intercept

    • The log

    • , etc.

  • Without further ado, since we are Spring Boot, we are of course using Spring AOP

Method of use

  • I’ll start with some common annotations for AOP
annotations explain
@Pointcut Usually used as@Pointcut("execution(* XXXXX.*.*(..) )"You can match the connection points and locate where you want to cut.
@Before Enhanced processing, which represents actions or methods that need to be executed before a pointcut can be executed
@After Represents what happens after a pointcut executes
@AfterReturning Pointcut /value: Specifies the entry expression of the pointcut. Returning: The value of this attribute specifies a parameter name that can be defined in an Advuce method with the same name that can be used to access the return value of the target method
@AfterThrowing Ditto, throwing, returns an exception
@Around Enhancements are equivalent to the sum of the above and are powerful but best used thread-safe
@DeclareParents Value: refers to the target object that you want to enhance. In this case, the UserServiceImpl object is enhanced. DefaultImpl: Specifies the class that introduced the enhanced feature

The actual use

  • Injection of pom

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-aop</artifactId>
                <version>2.2.2. RELEASE</version>
            </dependency>
    Copy the code
  • Let’s start with the section class

@Component
@Aspect
public class Aoptest {
    @Pointcut("execution(* com.example.zj.contorller.. *. * (..) )")
    public void pc1(a){}

    @Before(value = "pc1()")
    public void before(JoinPoint jp){
        String name = jp.getSignature().getName();
        System.out.println(name+"Method executed");
    }

    @After(value = "pc1()")
    public void after(JoinPoint jp){
        String name = jp.getSignature().getName();
        System.out.println(name+"Method execution completed");
    }

    @AfterReturning(value = "pc1()",returning = "result")
    public void afterReturning(JoinPoint jp,Object result){
        String name = jp.getSignature().getName();
        System.out.println(name+"Method return value is"+result);
    }

    @AfterThrowing(value = "pc1()",throwing = "e")
    public void afterThrowing(JoinPoint jp,Exception e){
        String name = jp.getSignature().getName();
        System.out.println(name+"Method throws an exception"+e.getMessage());
    }

    @Around("pc1()")
    public Object around(ProceedingJoinPoint pjp)throws Throwable{
        System.out.println("Method wrap first");
        try {
            Object o =  pjp.proceed();
            System.out.println("After the method is circled, the result is :" + o);
            return o;
        } catch (Throwable e) {
            e.printStackTrace();
            return null; }}}Copy the code
  • The controller class
@RestController
public class test {
    @GetMapping("test1")
    public JsonResult test1(a){
        return new JsonResult(200."sucess"."111");
    }
    @GetMapping("test2")
    public JsonResult test2(String name){
        if(name.length() < 3 || name.length() > 10)
        {
            throw new IllegalArgumentException("The name parameter must be longer than 3 and less than 10!");
        }
        return new JsonResult(200."sucess"."222"); }}Copy the code
  • Run the project, request the URL defined in the Controller, and the result is:
Method wrap firsttestJsonResult(code=200, MSG =sucess, result=111)test1 No further action is requiredtestJsonResult(code=200, MSG =sucess, result=111)Copy the code
  • You can request test2 as defined in the Controller and throw an exception to see the order of execution change

  • Finally tell me the code of execution (* com. Example. What zj had. Contorller.. *. * (..) ) :

    • The first * indicates the return type, and * is all types

    • This is followed by the package name.. And represents the current package and all subpackages

    • The * after is the class name, and the * is all the classes

    • The following * is the method

    • (..) Represents the method parameter.. And represents any parameters