@AspectJ註解方式的aop
阿新 • • 發佈:2021-11-28
第一步:匯入依賴
<dependencies>
<!-- 引入spring 依賴 作為CGLIB的依賴 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.12</version>
</dependency>
<!-- aspect -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.3.12</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>compile</scope>
</dependency>
<!--spring 整合測試-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.3.7</version>
<scope>test</scope>
</dependency>
</dependencies>
第三步:定義目標物件並注入到spring容器中
public interface ICustomerService {
public void save();
public int find();
public int error();
}
第四步:編寫通知類 並注入到spring容器中
package com.bird.advice.annotationaspectjaop.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
/**
* @data 2021/11/28 10:03
* @author: bird
* @description: 這個是基於註解的方式定義的通知類
*/