SpringBoot項(xiàng)目中使用AOP的方法
本文介紹了SpringBoot項(xiàng)目中使用AOP的方法,分享給大家,具體如下:
1.概述
將通用的邏輯用AOP技術(shù)實(shí)現(xiàn)可以極大的簡化程序的編寫,例如驗(yàn)簽、鑒權(quán)等。Spring的聲明式事務(wù)也是通過AOP技術(shù)實(shí)現(xiàn)的。
具體的代碼參照 示例項(xiàng)目 https://github.com/qihaiyan/springcamp/tree/master/spring-aop
Spring的AOP技術(shù)主要有4個核心概念:
Pointcut: 切點(diǎn),用于定義哪個方法會被攔截,例如 execution(* cn.springcamp.springaop.service.*.*(..))
Advice: 攔截到方法后要執(zhí)行的動作
Aspect: 切面,把Pointcut和Advice組合在一起形成一個切面
Join Point: 在執(zhí)行時Pointcut的一個實(shí)例
Weaver: 實(shí)現(xiàn)AOP的框架,例如 AspectJ 或 Spring AOP
2. 切點(diǎn)定義
常用的Pointcut定義有 execution 和 @annotation 兩種。execution 定義對方法無侵入,用于實(shí)現(xiàn)比較通用的切面。@annotation 可以作為注解加到特定的方法上,例如Spring的Transaction注解。
execution切點(diǎn)定義應(yīng)該放在一個公共的類中,集中管理切點(diǎn)定義。
示例:
public class CommonJoinPointConfig { @Pointcut("execution(* cn.springcamp.springaop.service.*.*(..))") public void serviceLayerExecution() {} }
這樣在具體的Aspect類中可以通過 CommonJoinPointConfig.serviceLayerExecution()來引用切點(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)需要改變時,只需修改CommonJoinPointConfig類即可,不用修改每個Aspect類。
3. 常用的切面
Before: 在方法執(zhí)行之前執(zhí)行Advice,常用于驗(yàn)簽、鑒權(quán)等。
After: 在方法執(zhí)行完成后執(zhí)行,無論是執(zhí)行成功還是拋出異常.
AfterReturning: 僅在方法執(zhí)行成功后執(zhí)行.
AfterThrowing: 僅在方法執(zhí)拋出異常后執(zhí)行.
一個簡單的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í)行時間,一種比較合理的方式是自定義一個注解,然后在需要收集執(zhí)行時間的方法上加上這個注解。
首先定義一個注解TrackTime:
@Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) public @interface TrackTime { String param() default ""; }
然后再定義一個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; } }
在某個方法上使用這個注解,就可以收集這個方法的執(zhí)行時間:
@TrackTime(param = "myService") public String runFoo() { System.out.println(" -------------> foo"); return "foo"; }
注意 @TrackTime(param = "myService") 注解是可以傳參的。
為了讓注解可以傳參數(shù),需要在定義注解時指定一個參數(shù)String param() default "默認(rèn)值",
同時在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)目時,控制臺會輸出以下內(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)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- springboot使用自定義注解實(shí)現(xiàn)aop切面日志
- SpringBoot使用AOP記錄接口操作日志詳解
- SpringBoot使用AOP實(shí)現(xiàn)統(tǒng)計(jì)全局接口訪問次數(shù)詳解
- 在springboot中使用AOP進(jìn)行全局日志記錄
- SpringBoot使用AOP,內(nèi)部方法失效的解決方案
- Springboot使用@Valid 和AOP做參數(shù)校驗(yàn)及日志輸出問題
- 詳解基于SpringBoot使用AOP技術(shù)實(shí)現(xiàn)操作日志管理
- SpringBoot使用AOP+注解實(shí)現(xiàn)簡單的權(quán)限驗(yàn)證的方法
- SpringBoot中使用AOP打印接口日志的方法
- Springboot 中使用 Aop代碼實(shí)戰(zhàn)教程
相關(guān)文章
IDEA報(bào)java:?java.lang.OutOfMemoryError:?Java?heap?space錯誤
這篇文章主要給大家介紹了關(guān)于IDEA報(bào)java:?java.lang.OutOfMemoryError:?Java?heap?space錯誤的解決辦法,文中將解決的辦法介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01如何使用mybatis-plus實(shí)現(xiàn)分頁查詢功能
最近在研究mybatis,然后就去找簡化mybatis開發(fā)的工具,發(fā)現(xiàn)就有通用Mapper和mybatis-plus兩個比較好的可是使用,可是經(jīng)過對比發(fā)現(xiàn)還是mybatis-plus比較好,下面這篇文章主要給大家介紹了關(guān)于如何使用mybatis-plus實(shí)現(xiàn)分頁查詢功能的相關(guān)資料,需要的朋友可以參考下2022-06-06JAVA使用JDBC連接oracle數(shù)據(jù)庫的詳細(xì)過程
JDBC是一種用于執(zhí)行SQL語句的Java API,可以為多種關(guān)系數(shù)據(jù)庫提供統(tǒng)一訪問,它由一組用Java語言編寫的類和接口組成,下面這篇文章主要給大家介紹了關(guān)于JAVA使用JDBC連接oracle數(shù)據(jù)庫的詳細(xì)過程,需要的朋友可以參考下2023-05-05深入淺出Java mvc_動力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了MVC的基礎(chǔ)知識,MVC是一個框架模式,它強(qiáng)制性的使應(yīng)用程序的輸入、處理和輸出分開,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08Java 實(shí)戰(zhàn)項(xiàng)目錘煉之在線購書商城系統(tǒng)的實(shí)現(xiàn)流程
讀萬卷書不如行萬里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+jsp+mysql+servlet+ajax實(shí)現(xiàn)一個在線購書商城系統(tǒng),大家可以在過程中查缺補(bǔ)漏,提升水平2021-11-11MybatisPlus多數(shù)據(jù)源及事務(wù)解決思路
這篇文章主要介紹了MybatisPlus多數(shù)據(jù)源及事務(wù)解決思路,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01java利用StringTokenizer分割字符串的實(shí)現(xiàn)
利用java.util.StringTokenizer的方法,可以將一個字符串拆分為一系列的標(biāo)記,本文就來介紹一下java利用StringTokenizer分割字符串的實(shí)現(xiàn),感興趣的可以了解一下2023-10-10基于Java和XxlCrawler獲取各城市月度天氣情況實(shí)踐分享
本文主要講解使用Java開發(fā)語言,使用XxlCrawler框架進(jìn)行智能的某城市月度天氣抓取實(shí)踐開發(fā),文章首先介紹目標(biāo)網(wǎng)站的相關(guān)頁面及目標(biāo)數(shù)據(jù)的元素,然后講解在信息獲取過程的一些參數(shù)配置以及問題應(yīng)對,需要的朋友可以參考下2024-05-05