本文介紹了SpringBoot項目中使用AOP的方法,分享給大家,具體如下:
1.概述
將通用的邏輯用AOP技術實現可以極大的簡化程序的編寫,例如驗簽、鑑權等。 Spring的聲明式事務也是通過AOP技術實現的。
具體的代碼參照示例項目https://github.com/qihaiyan/springcamp/tree/master/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@Componentpublic 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@Componentpublic 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)
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。