参考如何优雅地在 Spring Boot 中使用自定义注解,AOP 切面统一打印出入参日志 | 修订版

1.com.chanchifeng.springbootaopclasslog.config.AOPConfig

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import com.alibaba.fastjson.JSONObject;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;

@Configuration
@Aspect
public class AOPConfig {

@Around("@within(org.springframework.web.bind.annotation.RestController)")
public Object simpleAop(final ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
Object[] args = proceedingJoinPoint.getArgs();
System.out.println("入参: " + JSONObject.toJSONString(Arrays.asList(args)));
// 调用原有的方法
Object object = proceedingJoinPoint.proceed();
System.out.println("返参: " + object);
return object;
}
}

2.com.chanchifeng.springbootaopclasslog.controller.TestController

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import com.chanchifeng.springbootaopclasslog.form.UserForm;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

@GetMapping("/test")
public String test(String name) {
return "Hello, Aop !";
}

@PostMapping("/login")
public String login(@RequestBody UserForm userForm) {
return "Hello, Aop2 !";
}

}

3.com.chanchifeng.springbootaopclasslog.form.UserForm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import java.io.Serializable;

public class UserForm implements Serializable {
/**
* 用户名
*/
private String username;
/**
* 密码
*/
private String password;

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}
}

4.com.chanchifeng.springbootaopclasslog.SpringbootaopclasslogApplication

1
2
3
4
5
6
7
8
9
10
11
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootaopclasslogApplication {

public static void main(String[] args) {
SpringApplication.run(SpringbootaopclasslogApplication.class, args);
}

}

5.application.properties

1
spring.profiles.active=dev

6.pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?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 http://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.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.chanchifeng</groupId>
<artifactId>springbootaopclasslog</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springbootaopclasslog</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.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

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

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.31</version>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

7.运行效果如下:

项目的Github地址:https://github.com/porschan/springBootStudio/tree/master/springboot-aop-class-log