面試:Spring Boot專案怎麼用AOP
1.概述
將通用的邏輯用AOP技術實現可以極大的簡化程式的編寫,例如驗籤、鑑權
等。Spring的宣告式事務
也是通過AOP技術實現的。
Spring的AOP技術主要有4個核心概念:
-
Pointcut: 切點,用於定義哪個方法會被攔截,例如
execution(* cn.springcamp.springaop.service.*.*(..))
- Advice: 攔截到方法後要執行的動作
-
Aspect: 切面,把
Pointcut
和Advice
組合在一起形成一個切面 -
Join Point: 在執行時
Pointcut
的一個例項 -
Weaver: 實現AOP的框架,例如
AspectJ
或Spring AOP
2.切點定義
常用的Pointcut定義有execution
和@annotation
兩種。execution
定義對方法無侵入,用於實現比較通用的切面。@annotation
可以作為註解加到特定的方法上,例如Spring的Transaction
註解。
execution切點定義應該放在一個公共的類中,集中管理切點定義。
示例:
public class CommonJoinPointConfig { @Pointcut("execution(* cn.springcamp.springaop.service.*.*(..))") public void serviceLayerExecution() {} } 複製程式碼這樣在具體的Aspect類中可以通過 CommonJoinPointConfig.serviceLayerExecution()來引用切點。 public class BeforeAspect { @Before("CommonJoinPointConfig.serviceLayerExecution()") public void before(JoinPoint joinPoint) { System.out.println(" -------------> Before Aspect "); System.out.println(" -------------> before execution of " + joinPoint); } }
當切點需要改變時,只需修改CommonJoinPointConfig
類即可,不用修改每個Aspect
類。
3.常用的切面
-
Before: 在方法執行之
前
執行Advice,常用於驗籤、鑑權等
。 -
After: 在方法執行完成
後
執行,無論是執行成功還是丟擲異常
. -
AfterReturning: 僅在方法執行
成功後
執行. -
AfterThrowing: 僅在方法執丟擲
異常後
執行.
一個簡單的Aspect:
@Aspect @Component public class BeforeAspect { @Before("CommonJoinPointConfig.serviceLayerExecution()") public void before(JoinPoint joinPoint) { System.out.println(" -------------> Before Aspect "); System.out.println(" -------------> before execution of " + joinPoint); } }
4.自定義註解
假設我們想收集特定方法的執行時間,一種比較合理的方式是自定義一個註解,然後在需要收集執行時間的方法上加上這個註解。
首先定義一個註解TrackTime
:
@Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) public @interface TrackTime { String param() default ""; }
然後再定義一個Aspect
類,用於實現註解的行為:
@Aspect @Component public class TrackTimeAspect { @Around("@annotation(trackTime)") public Object around(ProceedingJoinPoint joinPoint, TrackTime trackTime) throws Throwable { Object result = null; long startTime = System.currentTimeMillis(); result = joinPoint.proceed(); long timeTaken = System.currentTimeMillis() - startTime; System.out.println(" -------------> Time Taken by " + joinPoint + " with param[" + trackTime.param() + "] is " + timeTaken); return result; } }
在某個方法上使用這個註解,就可以收集這個方法的執行時間:
@TrackTime(param = "myService") public String runFoo() { System.out.println(" -------------> foo"); return "foo"; }
注意@TrackTime(param = "myService")
註解是可以傳參的
為了讓註解可以傳引數,需要在定義註解時指定一個引數String param() default
"預設值",
同時在Aspect類中,around方法上加上相應的引數,@Around
註解中也需要用引數的變數名trackTime
,而不能用類名TrackTime
。
@Around("@annotation(trackTime)") public Object around(ProceedingJoinPoint joinPoint, TrackTime trackTime)
5.總結
在執行示例專案時,控制檯會輸出以下內容:
-------------> Before Aspect -------------> before execution of execution(String cn.springcamp.springaop.service.MyService.runFoo()) -------------> foo -------------> Time Taken by execution(String cn.springcamp.springaop.service.MyService.runFoo()) with param[myService] is 8 -------------> After Aspect -------------> after execution of execution(String cn.springcamp.springaop.service.MyService.runFoo()) -------------> AfterReturning Aspect -------------> execution(String cn.springcamp.springaop.service.MyService.runFoo()) returned with value foo
可以看出幾種 Aspect 的執行順序依次為 Before After Around AfterReturning(AfterThrowing)