Introduction to springboot aop

Keywords: Java Spring xml

Step 1: add dependency

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

Step 2: define a tangent class

package com.example.demo.aop;


import java.lang.reflect.Method;
import java.util.Arrays;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import static com.sun.xml.internal.ws.dump.LoggingDumpTube.Position.Before;

@Component
@Aspect // Will one java Class is defined as tangent class
@Order(-1)//If there are more than one aop,Priority can be defined here,Smaller, higher
public class LogDemo {
    private static final Logger LOG = LoggerFactory.getLogger(LogDemo.class);

    @Pointcut("execution(* com.example.demo.test.TestController.test(..))")//Two..For all subdirectories, the last two in parentheses..Represents all parameters
    public void logPointCut() {
    }

    @Before("logPointCut()")
    public void doBefore(JoinPoint joinPoint) throws Throwable {
        // Receive request, record request content
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        System.out.println("before");

    }

    @After(value = "logPointCut()")
    public void after(JoinPoint joinPoint) {
        System.out.println("after");
    }


    @AfterReturning(returning = "ret", pointcut = "logPointCut()")// returning Value sum doAfterReturning Consistent parameter names for
    public void doAfterReturning(Object ret) throws Throwable {
        System.out.println("AfterReturning");
    }

    @Around("logPointCut()")
    public void doAround(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("around1");
        Object ob = pjp.proceed();//The process method surrounding the notification cannot be omitted, otherwise it may not be executed
        System.out.println("around2");
    }
}

Be careful:

If two @ Before are defined in the same tangent class, the execution order of these two @ Before cannot be determined

For @ Around, no matter whether it has a return value or not, you must call PJP. Processed() inside the method; otherwise, the interface in the Controller will not be executed, and @ Before will not be triggered

 

The controller s tested are as follows:


package com.example.demo.test;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
public class TestController {

@RequestMapping(value = "test",method = RequestMethod.GET)
@ResponseBody
public String test(String name){
System.out.println("============method");
return name;
}
}
 

 

The configuration is completed and the output is as follows:

around1
before
============method
around2
after
AfterReturning

As you can see, the facet method performs as follows:

around-->before-->method-->around-->after-->AfterReturning

If @ AfterThrowing is configured, when there is an exception, execute as follows:

around-->before-->method-->around-->after-->AfterThrowing

Posted by morphy on Sat, 14 Dec 2019 09:22:22 -0800