SpringBoot中Aop拦截打印日志

											# 						Aop拦截打印日志

Aop相关的依赖

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

**以下为测试代码

测试的包目录

SpringBoot中Aop拦截打印日志_第1张图片

在相关包下建一个aop包,包里面建一个WebLogAspect的类(Aop测试代码中的核心代码)

//在com.itheima下建立的aop包WebLogAspect类
package com.itheima.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
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.Enumeration;

/**
 *
 *定义一个 切面 拦截 包
 */

@Aspect
@Component
@Slf4j
public class WebLogAspect {
    /**
     * 切入点
     * .*该包下的所有类
     * .*类中的所有方法
     * (..)方法中的所有参数
     */
    @Pointcut("execution(public * com.itheima.controller.*.*(..))")
    public void webLog() {
    }

    /**
     * 前置通知 请求方法之前做拦截
     *
     * @param joinPoint
     * @throws Throwable
     */
    @Before("webLog()")
    public void doBefore(JoinPoint joinPoint) throws Throwable {
        // 接收到请求,记录请求内容
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        // 记录下请求内容
        log.info("URL : " + request.getRequestURL().toString());
        log.info("HTTP_METHOD : " + request.getMethod());
        log.info("IP : " + request.getRemoteAddr());
        Enumeration<String> enu = request.getParameterNames();
        while (enu.hasMoreElements()) {
            String name = (String) enu.nextElement();
            log.info("name:{},value:{}", name, request.getParameter(name));
        }
    }

    /**
     * 目标方法请求之后 打印(响应)信息
     *
     * @param ret
     * @throws Throwable
     */
    @AfterReturning(returning = "ret", pointcut = "webLog()")
    public void doAfterReturning(Object ret) throws Throwable {
        // 处理完请求,返回内容
        log.info("RESPONSE : " + ret);
    }
}

其他测试中的代码

application.yml

myinfo:
  name: zhangsan
  project: springboot
  age: 20
server:
  port: 8082
  servlet:
#    设置springboot项目访问路劲
    context-path: /cen

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.itheima</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>ketangspringboot</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
        </dependency>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

pojo

//pojo包下的MyInfo类
package com.itheima.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Component
@ConfigurationProperties(prefix = "myinfo")
public class MyInfo {
    private String name;
    private String project;
    private int age;
}

controller

//controller包下的MyInfoController类
package com.itheima.controller;

import com.itheima.pojo.MyInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyInfoController {
    @Autowired
    private MyInfo myInfo;

    /**
     *
     * 演示打印日志
     * @return
     */
    @GetMapping("/myInfo")
    public String MyInfo() {
        return myInfo.toString();
    }
}

启动服务访问端口日志打印

@Autowired
private MyInfo myInfo;

/**
 *
 * 演示打印日志
 * @return
 */
@GetMapping("/myInfo")
public String MyInfo() {
    return myInfo.toString();
}

}


#### 启动服务访问端口日志打印

![在这里插入图片描述](https://img-blog.csdnimg.cn/e95da50c05c54216b79dd5808ba9247c.jpeg#pic_center)

你可能感兴趣的:(SpringBoot,spring,boot,java,spring)