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個(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ì)方法無侵入,用于實(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()來引用切點(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í)行,無論是執(zhí)行成功還是拋出異常.
AfterReturning: 僅在方法執(zhí)行成功后執(zhí)行.
AfterThrowing: 僅在方法執(zhí)拋出異常后執(zhí)行.
一個(gè)簡單的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)
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- SpringBoot中使用AOP打印接口日志的方法
- SpringBoot中創(chuàng)建的AOP不生效的原因及解決
- springboot?aop里的@Pointcut()的配置方式
- SpringBoot AOP方式實(shí)現(xiàn)多數(shù)據(jù)源切換的方法
- 詳解基于SpringBoot使用AOP技術(shù)實(shí)現(xiàn)操作日志管理
- springboot配置aop切面日志打印過程解析
- SpringBoot使用AOP實(shí)現(xiàn)統(tǒng)計(jì)全局接口訪問次數(shù)詳解
- SpringBoot中使用AOP實(shí)現(xiàn)日志記錄功能
- SpringBoot使用AOP+注解實(shí)現(xiàn)簡單的權(quán)限驗(yàn)證的方法
- SpringBoot中AOP的多種用途與實(shí)踐指南
相關(guān)文章
Java實(shí)現(xiàn)inputstream流的復(fù)制代碼實(shí)例
這篇文章主要介紹了Java實(shí)現(xiàn)inputstream流的復(fù)制代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-02-02
Java編程中的vector類用法學(xué)習(xí)筆記
Vector通常被用來實(shí)現(xiàn)動(dòng)態(tài)數(shù)組,即可實(shí)現(xiàn)自動(dòng)增長的對(duì)象數(shù)組,和C++一樣vector類同樣被Java內(nèi)置,下面就來看一下vector類的基本用法.2016-05-05
EasyExcel如何導(dǎo)出多級(jí)且動(dòng)態(tài)的表頭
文章介紹了使用EasyExcel庫實(shí)現(xiàn)Excel多級(jí)表頭導(dǎo)出的步驟,包括創(chuàng)建表頭集合、導(dǎo)出數(shù)據(jù)集合、設(shè)置樣式和列寬,以及自動(dòng)合并表頭和測(cè)試導(dǎo)出2026-01-01
spring boot項(xiàng)目快速構(gòu)建的全步驟
這篇文章主要給大家介紹了關(guān)于spring boot項(xiàng)目快速構(gòu)建的全步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用spring boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
mall整合SpringSecurity及JWT認(rèn)證授權(quán)實(shí)戰(zhàn)下
這篇文章主要為大家介紹了mall整合SpringSecurity及JWT認(rèn)證授權(quán)實(shí)戰(zhàn)第二篇,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
Java 獲取本機(jī)的IP與MAC地址實(shí)現(xiàn)詳解
這篇文章主要介紹了Java 獲取本機(jī)的IP與MAC地址實(shí)現(xiàn)詳解的相關(guān)資料,需要的朋友可以參考下2016-09-09

