SpringBoot AOP切面编程步骤
1. 添加maven依赖
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-aop -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<version>2.7.0</version>
</dependency>
2.添加MyAOP.class类
@Aspect
@Component
public class MyAOP {
}
3. 添加切入点
@Aspect
@Component
public class MyAOP {
@Pointcut(value = "execution(* com.china.beijing.module.user.controller.*.*(..))")
public void myPoint(){
/*todo */
}
}
4.添加相应的处理方法
@Aspect
@Component
public class MyAOP {
@Pointcut(value = "execution(* com.china.beijing.module.user.controller.*.*(..))")
public void myPoint(){
/*todo */
}
@Before("myPoint()")
public void beforeHandle(){
//todo 切面运行之前
}
@After("myPoint()")
public void afterHandle(){
//todo 切面运行之后
}
@Around("myPoint()")
public void aroundHandle(ProceedingJoinPoint pjp) throws Throwable {
//todo 运行之前
Object proceed = pjp.proceed();
//todo 运行之后
}
}