SpringBoot項(xiàng)目中使用AOP的方法
本文介紹了SpringBoot項(xiàng)目中使用AOP的方法,分享給大家,具體如下:
1.概述
將通用的邏輯用AOP技術(shù)實(shí)現(xiàn)可以極大的簡(jiǎn)化程序的編寫(xiě),例如驗(yàn)簽、鑒權(quán)等。Spring的聲明式事務(wù)也是通過(guò)AOP技術(shù)實(shí)現(xiàn)的。
具體的代碼參照 示例項(xiàng)目 https://github.com/qihaiyan/springcamp/tree/master/spring-aop
Spring的AOP技術(shù)主要有4個(gè)核心概念:
Pointcut: 切點(diǎn),用于定義哪個(gè)方法會(huì)被攔截,例如 execution(* cn.springcamp.springaop.service.*.*(..))
Advice: 攔截到方法后要執(zhí)行的動(dòng)作
Aspect: 切面,把Pointcut和Advice組合在一起形成一個(gè)切面
Join Point: 在執(zhí)行時(shí)Pointcut的一個(gè)實(shí)例
Weaver: 實(shí)現(xiàn)AOP的框架,例如 AspectJ 或 Spring AOP
2. 切點(diǎn)定義
常用的Pointcut定義有 execution 和 @annotation 兩種。execution 定義對(duì)方法無(wú)侵入,用于實(shí)現(xiàn)比較通用的切面。@annotation 可以作為注解加到特定的方法上,例如Spring的Transaction注解。
execution切點(diǎn)定義應(yīng)該放在一個(gè)公共的類中,集中管理切點(diǎn)定義。
示例:
public class CommonJoinPointConfig {
@Pointcut("execution(* cn.springcamp.springaop.service.*.*(..))")
public void serviceLayerExecution() {}
}
這樣在具體的Aspect類中可以通過(guò) CommonJoinPointConfig.serviceLayerExecution()來(lái)引用切點(diǎn)。
public class BeforeAspect {
@Before("CommonJoinPointConfig.serviceLayerExecution()")
public void before(JoinPoint joinPoint) {
System.out.println(" -------------> Before Aspect ");
System.out.println(" -------------> before execution of " + joinPoint);
}
}
當(dāng)切點(diǎn)需要改變時(shí),只需修改CommonJoinPointConfig類即可,不用修改每個(gè)Aspect類。
3. 常用的切面
Before: 在方法執(zhí)行之前執(zhí)行Advice,常用于驗(yàn)簽、鑒權(quán)等。
After: 在方法執(zhí)行完成后執(zhí)行,無(wú)論是執(zhí)行成功還是拋出異常.
AfterReturning: 僅在方法執(zhí)行成功后執(zhí)行.
AfterThrowing: 僅在方法執(zhí)拋出異常后執(zhí)行.
一個(gè)簡(jiǎn)單的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. 自定義注解
假設(shè)我們想收集特定方法的執(zhí)行時(shí)間,一種比較合理的方式是自定義一個(gè)注解,然后在需要收集執(zhí)行時(shí)間的方法上加上這個(gè)注解。
首先定義一個(gè)注解TrackTime:
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface TrackTime {
String param() default "";
}
然后再定義一個(gè)Aspect類,用于實(shí)現(xiàn)注解的行為:
@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;
}
}
在某個(gè)方法上使用這個(gè)注解,就可以收集這個(gè)方法的執(zhí)行時(shí)間:
@TrackTime(param = "myService")
public String runFoo() {
System.out.println(" -------------> foo");
return "foo";
}
注意 @TrackTime(param = "myService") 注解是可以傳參的。
為了讓注解可以傳參數(shù),需要在定義注解時(shí)指定一個(gè)參數(shù)String param() default "默認(rèn)值",
同時(shí)在Aspect類中,around方法上加上相應(yīng)的參數(shù),@Around注解中也需要用參數(shù)的變量名trackTime,而不能用類名TrackTime。
@Around("@annotation(trackTime)")
public Object around(ProceedingJoinPoint joinPoint, TrackTime trackTime)
5.總結(jié)
在運(yùn)行示例項(xiàng)目時(shí),控制臺(tái)會(huì)輸出以下內(nèi)容:
-------------> 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 的執(zhí)行順序依次為 Before After Around AfterReturning(AfterThrowing)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- springboot使用自定義注解實(shí)現(xiàn)aop切面日志
- SpringBoot使用AOP記錄接口操作日志詳解
- SpringBoot使用AOP實(shí)現(xiàn)統(tǒng)計(jì)全局接口訪問(wèn)次數(shù)詳解
- 在springboot中使用AOP進(jìn)行全局日志記錄
- SpringBoot使用AOP,內(nèi)部方法失效的解決方案
- Springboot使用@Valid 和AOP做參數(shù)校驗(yàn)及日志輸出問(wèn)題
- 詳解基于SpringBoot使用AOP技術(shù)實(shí)現(xiàn)操作日志管理
- SpringBoot使用AOP+注解實(shí)現(xiàn)簡(jiǎn)單的權(quán)限驗(yàn)證的方法
- SpringBoot中使用AOP打印接口日志的方法
- Springboot 中使用 Aop代碼實(shí)戰(zhàn)教程
相關(guān)文章
IntelliJ IDEA 2020.3 重大特性(新功能一覽)
這篇文章主要介紹了IntelliJ IDEA 2020.3 重大特性(新功能一覽),本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12
Mybatis一對(duì)一延遲加載實(shí)現(xiàn)過(guò)程解析
這篇文章主要介紹了Mybatis一對(duì)一延遲加載實(shí)現(xiàn)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10
java中JVM中如何存取數(shù)據(jù)和相關(guān)信息詳解
這篇文章主要介紹了JVM中如何存取數(shù)據(jù)和相關(guān)信息詳解,Java源代碼文件(.java后綴)會(huì)被Java編譯器編譯為字節(jié)碼文件,然后由JVM中的類加載器加載各個(gè)類的字節(jié)碼文件,加載完畢之后,交由JVM執(zhí)行引擎執(zhí)行。JVM中怎么存取數(shù)據(jù)和相關(guān)信息呢?,需要的朋友可以參考下2019-06-06
簡(jiǎn)單了解Java方法的定義和使用實(shí)現(xiàn)詳解
這篇文章主要介紹了簡(jiǎn)單了解Java方法的定義和使用實(shí)現(xiàn)詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12
Springboot使用pdfbox提取PDF圖片的代碼示例
PDFBox是一個(gè)用于創(chuàng)建和處理PDF文檔的Java庫(kù),它可以使用Java代碼創(chuàng)建、讀取、修改和提取PDF文檔中的內(nèi)容,本文就給大家介紹Springboot如何使用pdfbox提取PDF圖片,感興趣的同學(xué)可以借鑒參考2023-06-06
Java基礎(chǔ)之線程鎖相關(guān)知識(shí)總結(jié)
今天給大家?guī)?lái)的是關(guān)于Java線程的相關(guān)知識(shí),文章圍繞著Java線程鎖展開(kāi),文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06
Spring Security 中如何讓上級(jí)擁有下級(jí)的所有權(quán)限(案例分析)
這篇文章主要介紹了Spring Security 中如何讓上級(jí)擁有下級(jí)的所有權(quán)限,本文通過(guò)案例分析給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
Spring的refresh()方法相關(guān)異常解析
這篇文章主要介紹了Spring的refresh()方法相關(guān)異常解析,具有一定參考價(jià)值,需要的朋友可以了解下。2017-11-11

