另见《深入浅出Spring Boot 2.x》=> 4.3 AOP开发详解
因为AOP可以将切点设为注解类,因此,若想控制Controller,只需要控制@RequestMapping,即:
@Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping)")本文工程全部源码:https://gitee.com/shapeless/demo_JAVA/tree/master/demo_SpringAOP
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.3.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo_springaop-simple</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo_springaop-simple</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-aop</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <!-- <exclusions>--> <!-- <exclusion>--> <!-- <groupId>org.junit.vintage</groupId>--> <!-- <artifactId>junit-vintage-engine</artifactId>--> <!-- </exclusion>--> <!-- </exclusions>--> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.20</version> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>启动类
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoSpringaopSimpleApplication { public static void main(String[] args) { SpringApplication.run(DemoSpringaopSimpleApplication.class, args); } }Entity
package com.example.demo.entity; import lombok.Data; @Data public class User { private Integer id; private String userName; private String note; }Service
package com.example.demo.service; import com.example.demo.entity.User; public interface UserService { public void printUser(User user); } package com.example.demo.service.impl; import com.example.demo.entity.User; import com.example.demo.service.UserService; import org.springframework.stereotype.Service; @Service public class UserServiceImpl implements UserService { @Override public void printUser(User user) { if (user == null) { throw new RuntimeException("检查用户参数是否为空"); } System.out.print("id = " + user.getId()); System.out.print("\t userName = " + user.getUserName()); System.out.println("\t note = " + user.getNote()); } }Controller
package com.example.demo.controller; import com.example.demo.annotation.OperationLog; import com.example.demo.entity.User; import com.example.demo.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @RequestMapping("/user") @Controller public class UserController { @Autowired UserService userService; @RequestMapping("/print") @ResponseBody public User testPrint(User user) { userService.printUser(user); return user; } @RequestMapping("/add") @ResponseBody @OperationLog(type = "添加", desc = "添加数据") public User testAdd(User user) { return user; } @RequestMapping("/update") @ResponseBody @OperationLog(type = "更新", desc = "更新数据") public User testUpdate(User user) { userService.printUser(user); return user; } }测试
postman访问:http://localhost:8080/user/add?id=321&userName=Tony¬e=abc
后端结果
joinPoint.getTarget().toString() : com.example.demo.controller.UserController@10852612 methodSignature.getName() : testAdd method.getName() : testAdd method.getReturnType().getName() : com.example.demo.entity.User ----------------------------------------------- type: 添加 desc: 添加数据 ----------------------------------------------- User(id=321, userName=Tony, note=abc) id是:321; 名字是:Tony; 备注是:abcpostman结果
{ "id": 321, "userName": "Tony", "note": "abc" }postman访问:http://localhost:8080/user/update?id=321&userName=Tony¬e=abc
后端结果
joinPoint.getTarget().toString() : com.example.demo.controller.UserController@10852612 methodSignature.getName() : testUpdate method.getName() : testUpdate method.getReturnType().getName() : com.example.demo.entity.User ----------------------------------------------- id = 321 userName = Tony note = abc type: 更新 desc: 更新数据 ----------------------------------------------- User(id=321, userName=Tony, note=abc) id是:321; 名字是:Tony; 备注是:abcpostman结果
{ "id": 321, "userName": "Tony", "note": "abc" }测试
postman访问:http://localhost:8080/user/add?id=321&userName=Tony¬e=abc
后端结果
joinPoint.getTarget().toString() : com.example.demo.controller.UserController@36efe205 methodSignature.getName() : testAdd method.getName() : testAdd method.getReturnType().getName() : com.example.demo.entity.User ----------------------------------------------- method.getName(): testAdd type: 添加 desc: 添加数据 ----------------------------------------------- User(id=321, userName=Tony, note=abc) id是:321; 名字是:Tony; 备注是:abcpostman结果
{ "id": 321, "userName": "Tony", "note": "abc" }postman访问:http://localhost:8080/user/update?id=321&userName=Tony¬e=abc
后端结果
joinPoint.getTarget().toString() : com.example.demo.controller.UserController@36efe205 methodSignature.getName() : testUpdate method.getName() : testUpdate method.getReturnType().getName() : com.example.demo.entity.User ----------------------------------------------- id = 321 userName = Tony note = abc method.getName(): testUpdate type: 更新 desc: 更新数据 ----------------------------------------------- User(id=321, userName=Tony, note=abc) id是:321; 名字是:Tony; 备注是:abcpostman结果
{ "id": 321, "userName": "Tony", "note": "abc" }测试
postman访问:http://localhost:8080/user/add?id=321&userName=Tony¬e=abc
后端结果:
joinPoint.getTarget().toString() : com.example.demo.controller.UserController@71e616a3 methodSignature.getName() : testAdd method.getName() : testAdd method.getReturnType().getName() : com.example.demo.entity.User ----------------------------------------------- type: 添加 desc: 添加数据 ----------------------------------------------- User(id=321, userName=Tony, note=abc) id是:321; 名字是:Tony; 备注是:abcpostman结果
无
postman访问:http://localhost:8080/user/update?id=321&userName=Tony¬e=abc
后端结果:
joinPoint.getTarget().toString() : com.example.demo.controller.UserController@71e616a3 methodSignature.getName() : testUpdate method.getName() : testUpdate method.getReturnType().getName() : com.example.demo.entity.User ----------------------------------------------- id = 321 userName = Tony note = abc type: 更新 desc: 更新数据 ----------------------------------------------- User(id=321, userName=Tony, note=abc) id是:321; 名字是:Tony; 备注是:abcpostman结果
无