Spring Boot 使用AOP
- 在pom文件中添加AOP依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
- 添加切入点类
@Component
@Aspect
public class LogAop {
@Before("execution(* com.aimilin.demo..*.*(..))")
public void before() {
System.out.println("start ---- log");
}
@After("execution(* com.aimilin.demo..*.*(..))")
public void after(JoinPoint joinPoint) {
System.out.println(
"after --------log, Class: " + joinPoint.getTarget().getClass() + "\n" +
"Method: " + joinPoint.getSignature().getName() + "\n" +
"Args :" + Arrays.asList(joinPoint.getArgs()));
}
}
- Spring Boot Aop常用配置
配置文件位置: AopAutoConfiguration
spring.aop.auto=true
spring.aop.proxy-target-class=true
4.