AOP基本總結
-
連接點(JoinPoint):
- 連接點是程式運行的某個階段點,如方法呼叫、例外拋出等
-
切入點(Pointcut):
- 切入點是JoinPoint的集合
- 是程式中需要注入Advice的位置的集合,即Advice在什么條件下才能被觸發
-
增強(Advisor):
- 增強是切入點Pointcut和Advice的綜合體,即在連接點JoinPoint上執行的行為
- 通過JDK/CGLIB代理模式實作AOP
-
切面(Aspect):
- @Aspect通常是一個類的注解,通常與@Component搭配使用
-
AOP代理(AOP Proxy):
- AOP使用動態代理模式創建物件,從而實作在連接點JoinPoint處插入增強
- 其中JDK只能代理介面,CGLIB基于子類但不能代理final類
常用方法
| 方法 | 描述 |
| Object[] getArgs | 回傳目標方法的引數陣列 |
| Signature getSignature | 回傳目標方法所在類資訊 |
| Object getTarget | 回傳被織入增強處理的目標物件 |
| Object getThis |
回傳AOP代理物件 |
| Object proceed(Object[] args) |
利用反射執行目標方法并回傳結果 |
增強型別
-
@Before:前置增強,在某個JoinPoint執行前的增強
-
@After:final增強,不管拋例外還是正常退出都執行的增強
-
@AfterReturning:后置增強,方法正常退出時執行
-
@AfterThrowing:例外拋出增強,拋出例外后執行
-
@Around:環繞增強,包圍一個連接點的增強,最強大的一個方式,且常用
示例說明
學了一下AOP的使用,寫了個@Around的demo,將幾個查詢操作存入資料庫作為Log并且定時清理過期資料
本人的Demo用的是Dubbo框架,而AOP的示例寫在了Provider中,大概結構如下:

- monitor:
- annotation:注解類
- aop:切面的定義及實作
- impl:UserAopTask介面的實作類
1)UserLog物體類
@Data
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class UserLog implements Serializable {
private Integer id;
private String methodName;
private String methodArgs;
private String classFullName;
private String className;
private Date invokeTime;
private Double costTime;
}
2)LogTaskMapper對應mapper介面
public interface LogTaskMapper {
/**
* TEST AOP INSERT INFO INTO TABLE
* @param userLog
*/
void insertUserLog(UserLog userLog);
/**
* DELETE LOGS IN TABLE LAST x MINUTES
* @param minutes
*/
void deleteUserLog(int minutes);
}
3)UserAopTask介面
public interface UserAopTask {
void insertUserLog(UserLog log);
}
4)UserAopTaskImpl實作類
@Component
public class UserAopTaskImpl implements UserAopTask {
private static final Logger logger = LoggerFactory.getLogger(UserAopTask.class);
@Autowired
private LogTaskMapper logTaskMapper;
private ExecutorService logHandler = Executors.newFixedThreadPool(1);//采用執行緒池復用一個執行緒執行
private static final int MINUTES_LOG_RETAIN = 30;//資料庫中資料保留時間
@Override
public void insertUserLog(UserLog log) {
logHandler.submit(new logSubmitTask(log));
}
//內部類
class logSubmitTask implements Runnable{
private UserLog userLog;
public logSubmitTask(UserLog userLog){
this.userLog = userLog;
}
@Override
public void run() {
logTaskMapper.insertUserLog(userLog);
}
}
//定時清理任務
@Scheduled(cron = "0 30 * * * *")
public void scheduledDeleteLog(){
logger.info("開始清除[{}]分鐘之前的圖表查詢日志...", MINUTES_LOG_RETAIN);
logTaskMapper.deleteUserLog(-1 * MINUTES_LOG_RETAIN);
}
}
5)TestUserAop切面類
@Aspect//切面
@Component//Spring容器管理
public class TestUserAop {
private static final Logger logger = LoggerFactory.getLogger(TestUserAop.class);
@Autowired
private UserAopTask userAopTask;
//使用環繞增強,第一引數必須是ProceedingJoinPoint
@Around(value = "https://www.cnblogs.com/torima/archive/2021/09/13/@annotation(annotation)")//和注解類引數名保持一致
public Object aroundUserInfo(ProceedingJoinPoint pjp, TestUserAnnotation annotation) throws Throwable{
UserLog userLog = new UserLog();
System.out.println("=====================ANNOTATION BEGIN=====================");
Date date = new Date();
Long methodStart = date.getTime();//timestamp
System.out.println("ANNOTATION 開始耗時統計: "+ date);
userLog.setInvokeTime(date);
Object[] argsObj = pjp.getArgs();
Object res = pjp.proceed(argsObj);//利用反射呼叫目標方法
Long methodCost = System.currentTimeMillis() - methodStart;
double cost = methodCost/1000d;//timestamp 轉換為 seconds
System.out.println("ANNOTATION 呼叫方法總耗時: "+ String.format("%.3f",cost) +" s");//保留3位小數
System.out.println("ANNOTATION 呼叫方法: "+annotation.methodName());//目標方法
System.out.println("ANNOTATION 呼叫方法引數: "+ new Integer((Integer) argsObj[0]));//我的引數就1個或者無參
System.out.println("ANNOTATION 呼叫類: "+pjp.getSignature().getDeclaringTypeName());//全類名
System.out.println("ANNOTATION 呼叫類名: "+pjp.getSignature().getDeclaringType().getSimpleName());//類名
System.out.println("ANNOTATION 呼叫結果: "+ JSON.toJSON(res));
System.out.println("=====================ANNOTATION FINISHED=====================");
userLog.setCostTime(Double.parseDouble(String.format("%.3f",cost)));
userLog.setClassFullName(pjp.getSignature().getDeclaringTypeName());
userLog.setClassName(pjp.getSignature().getDeclaringType().getSimpleName());
userLog.setMethodName(annotation.methodName());
userLog.setMethodArgs(Integer.toString(new Integer((Integer) argsObj[0])));
userAopTask.insertUserLog(userLog);
return res;
}
}
6)TestUserAnnotation注解類
我在service層寫的AOP demo,對目標方法使用注解,注解名為注解類名即可,如@TestUserAnnotation
@Retention(RetentionPolicy.RUNTIME)//運行時有效
@Target(ElementType.METHOD)//作用于方法
@Documented
public @interface TestUserAnnotation {
String methodName() default "";//方法名,默認為空字串
}
7)LogTaskMapper.xml
最后貼個代碼,為上面提到的定時任務,用到的是date_add()方法,其中的 "<" 意為 "<"
<delete id="deleteUserLog" parameterType="java.lang.Integer">
delete from invoke_log
where invoke_time < date_add(current_timestamp,interval #{minutes} minute)
</delete>
結果展示
演示一下AOP的效果,將@TestUserAnnotation注解在方法getUserInfo(),即獲取用戶資訊
Demo中利用AOP的@Around環繞增強,實作了統計方法呼叫運行消耗時間,以及統計呼叫方法名、類名等資訊:

呼叫方法getUserInfo后的統計結果:

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/299844.html
標籤:其他
下一篇:各種運算子的使用
