SpringBoot項(xiàng)目中使用AOP的方法
本文介紹了SpringBoot項(xiàng)目中使用AOP的方法,分享給大家,具體如下:
1.概述
將通用的邏輯用AOP技術(shù)實(shí)現(xiàn)可以極大的簡(jiǎ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個(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類中可以通過 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ì)全局接口訪問次數(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)簡(jiǎn)單的權(quán)限驗(yàn)證的方法
- SpringBoot中使用AOP打印接口日志的方法
- Springboot 中使用 Aop代碼實(shí)戰(zhàn)教程
相關(guān)文章
IDEA報(bào)java:?java.lang.OutOfMemoryError:?Java?heap?space錯(cuò)誤
這篇文章主要給大家介紹了關(guān)于IDEA報(bào)java:?java.lang.OutOfMemoryError:?Java?heap?space錯(cuò)誤的解決辦法,文中將解決的辦法介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01
如何使用mybatis-plus實(shí)現(xiàn)分頁(yè)查詢功能
最近在研究mybatis,然后就去找簡(jiǎn)化mybatis開發(fā)的工具,發(fā)現(xiàn)就有通用Mapper和mybatis-plus兩個(gè)比較好的可是使用,可是經(jīng)過對(duì)比發(fā)現(xiàn)還是mybatis-plus比較好,下面這篇文章主要給大家介紹了關(guān)于如何使用mybatis-plus實(shí)現(xiàn)分頁(yè)查詢功能的相關(guān)資料,需要的朋友可以參考下2022-06-06
JAVA使用JDBC連接oracle數(shù)據(jù)庫(kù)的詳細(xì)過程
JDBC是一種用于執(zhí)行SQL語(yǔ)句的Java API,可以為多種關(guān)系數(shù)據(jù)庫(kù)提供統(tǒng)一訪問,它由一組用Java語(yǔ)言編寫的類和接口組成,下面這篇文章主要給大家介紹了關(guān)于JAVA使用JDBC連接oracle數(shù)據(jù)庫(kù)的詳細(xì)過程,需要的朋友可以參考下2023-05-05
深入淺出Java mvc_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了MVC的基礎(chǔ)知識(shí),MVC是一個(gè)框架模式,它強(qiáng)制性的使應(yīng)用程序的輸入、處理和輸出分開,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
Java 實(shí)戰(zhàn)項(xiàng)目錘煉之在線購(gòu)書商城系統(tǒng)的實(shí)現(xiàn)流程
讀萬(wàn)卷書不如行萬(wàn)里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+jsp+mysql+servlet+ajax實(shí)現(xiàn)一個(gè)在線購(gòu)書商城系統(tǒng),大家可以在過程中查缺補(bǔ)漏,提升水平2021-11-11
MybatisPlus多數(shù)據(jù)源及事務(wù)解決思路
這篇文章主要介紹了MybatisPlus多數(shù)據(jù)源及事務(wù)解決思路,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
java利用StringTokenizer分割字符串的實(shí)現(xiàn)
利用java.util.StringTokenizer的方法,可以將一個(gè)字符串拆分為一系列的標(biāo)記,本文就來(lái)介紹一下java利用StringTokenizer分割字符串的實(shí)現(xiàn),感興趣的可以了解一下2023-10-10
基于Java和XxlCrawler獲取各城市月度天氣情況實(shí)踐分享
本文主要講解使用Java開發(fā)語(yǔ)言,使用XxlCrawler框架進(jìn)行智能的某城市月度天氣抓取實(shí)踐開發(fā),文章首先介紹目標(biāo)網(wǎng)站的相關(guān)頁(yè)面及目標(biāo)數(shù)據(jù)的元素,然后講解在信息獲取過程的一些參數(shù)配置以及問題應(yīng)對(duì),需要的朋友可以參考下2024-05-05

