1. Spring Boot中的AOP
⏰ 关键代码
- 🌏 使用
@Aspect
和@Component
注解标记类为切面,并使用@Pointcut
定义切入点,使用@Around
定义通知@Aspect @Component @Slf4j public class AopTestSimple { /** * 定义切入点 */ @Pointcut("execution(public * cn.ohbee.demoaop.*Controller.*(..))") public void aopTestSimplePointcut() { } /** * 定义环绕通知,①解析浏览器请求信息,②获取调取方法的信息,③获取方法执行完的返回结果 * @param point 通知的第一个固有参数 */ @Around("aopTestSimplePointcut()") public Object aopTestSimpleAround(ProceedingJoinPoint point) throws Throwable { //目标方法执行 Object proceed = point.proceed(); ... return proceed; } }
⏰ Maven依赖
- 🌏 pom.xml 中使用如下依赖,如下:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--web 项目依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--AOP 项目依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>