Spring Boot 使用AOP

Spring Boot 使用AOP

  1. 在pom文件中添加AOP依賴

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>
  1. 添加切入點類
@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()));
    }
}
  1. Spring Boot Aop常用配置
    配置文件位置: AopAutoConfiguration
# 是否啓用AOP,默認啓用
spring.aop.auto=true 

# true 使用JDK代理(類需要有接口),false - cgLib代理
spring.aop.proxy-target-class=true
    4.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章