Cover author: yemoze1314, licensed for use.

A, problem,

1.1, the environment

Computer environment: Windows 10

Development tools: IntelliJ IDEA;

JDK environment: Jdk1.8;

1.2, problem,

Because I want to see every incoming and outgoing parameter requested, I wrote a log printing method combined with SpringBoot AOP(faceted programming); So how does that work?

Second, the answer

1. Jar package dependencies, the following three dependency packages are the necessary conditions for the implementation of the aspect function;

<! -- Added by Taozhouchuan at face cutting2020-8-3 19:26:53   :http://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.89.</version> </dependency> <! -- http://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.89.</version> </dependency> <! -- http://mvnrepository.com/artifact/cglib/cglib -->
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>3.22.</version>
        </dependency>
Copy the code

例 句 : AOP’s point Cut

private final String POINT_CUT = "execution(public * com.demo.controller.*.*(..) )";
Copy the code

3. There are five main AOP pointcuts of Spring:

package com.demo.filter;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/** * interceptor *@author tzc
 */
@Aspect
@Component
@Slf4j
public class LogIntercept {

    ThreadLocal<Long> startTime = new ThreadLocal<Long>();

    private final String POINT_CUT = "execution(public * com.demo.controller.*.*(..) )";

    /** * in the pointcut expression,.. Two dots indicate multiple, * represents one, and the above expression represents all methods that cut into all classes in the com.demo.controller package. * method parameters are unlimited, and return types are unlimited. The first * indicates that the return type is unlimited, the second * indicates that all classes, the third * indicates that all methods, and *.. The two dots indicate that the parameters in the method are unlimited. And then use@PointcutPointcut annotation, want above an empty method, */
    /** * specifies the cut surface, which can be an empty method; * /
    @Pointcut(POINT_CUT)
    private void pointCut(a) {}// Print the content before requesting method
    @Before(value = "pointCut()")
    public void before(JoinPoint joinPoint) {
        startTime.set(System.currentTimeMillis());

        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();

        // Prints the request
        log.error("=================[Before] Request content ===============");
        log.info("==================[Before] request address :{}", request.getRequestURL().toString());
        log.info("==================[Before] request mode :{}", request.getMethod());
        log.info("==================[Before] Request class method :{}", joinPoint.getSignature());
        log.info("==================[Before] Request class method parameter :{}", Arrays.toString(joinPoint.getArgs()));
        log.info("==================[Before] Request content ===============");
    }

   
    // Wrap around: defines the pointcut expression that needs to be matched, along with the parameters that need to be matched
    @Around(value = "pointCut()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        log.info("= = = = = = = = = = = = = = = = = = [Around] way Around the front start = = = = = = = = = = = = = = = = = = = = = >");
        // This sentence must have a continuation method
        Object result = pjp.proceed();

        log.info("= = = = = = = = = = = = = = = = = = [Around] method Around the rear start = = = = = = = = = = = = = = = = = = = = = >");
        log.info("==================[Around] : {}", (System.currentTimeMillis() - startTime.get()));
        log.info("==================[Around] return data: {}", result);
        log.info("= = = = = = = = = = = = = = = = = = [Around] method Around end = = = = = = = = = = = = = = = = = = = = = = >");
        return result;
    }

	// after the method is executed
    @After(value = "pointCut()")
    public void after(a){
        log.debug("==================[After] After execution of pointcut method");
    }

	// Method exception interception
    @AfterThrowing(value = "pointCut()")
    public void afterThrowing(a){
        log.debug("==================[AfterThrowing] pointcut method throws an exception");
    }


    // Print the returned content after the method completes execution
    @AfterReturning(returning = "o", pointcut = "pointCut()")
    public void afterReturning(Object o) {
        log.info("= = = = = = = = = = = = = = = = = = [AfterReturning] content of the Response:" + JSON.toJSONString(o));
        log.info("==================[AfterReturning] : {}ms", (System.currentTimeMillis() - startTime.get())); }}Copy the code

Thus, a zero-to-functional log-printing interceptor is complete; The console logs are as follows:

End ~

Third, summary

If you follow the above configuration method, if still not blocked (because no interception will not report an error), so please focus on the above three angles:

1. Whether the JAR package is correctly referenced in the POM.xml file, or the Library package is correctly downloaded and associated with the project;

2. Check whether the intercept path of pointCut execute is correct.

3. Is there any bug in Java code?

Welcome to mine

The Denver nuggets: juejin. Cn/user / 288558…

CSDN blog: blog.csdn.net/River_Conti…

Wechat official account: Muqiao Community

Zhihu: zhang makino, www.zhihu.com/people/zhan…

Jane: www.jianshu.com/u/02c0096cb…