SpringBoot AOP使用筆記
1. 啟用AOP
a. 在類上添加@Aspect注解
b. 注入該類, 可以使用@Component進(jìn)行注入到Spring容器中
2. 通過(guò)PointCut對(duì)象創(chuàng)建切入點(diǎn)
a. 在某個(gè)方法使用類似下面的方法進(jìn)行注入
@Pointcut("execution(* com.sguess.service.IAOPService.*(..))") private void pointcut() { }
i. 其中,execution表達(dá)式為
execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern)throws-pattern?)
ii. 注意, pointcut()方法名是后面切入的時(shí)候需要使用的
iii. 方法內(nèi)可以什么也不寫(xiě), 寫(xiě)了也調(diào)不到
iv. 也可以創(chuàng)建多個(gè)PointCut,例如再創(chuàng)建一個(gè)
@Pointcut("execution(* com.sguess.service.IAOPService.fun1(..))") private void pointcut2() { }
這個(gè)的方法名就位pointcut2, 方法名不一樣.
b. 創(chuàng)建After方法,Before方法
@After(value = "pointcut()") public void doAfter() { System.out.println("Do AOP After function 01"); }
i. After方法是指, 在配置了的切入點(diǎn)被執(zhí)行后, 執(zhí)行該方法.
ii. value中的pointcut() 是我們前面在創(chuàng)建@Pointcut中的方法名. 也就是說(shuō),是通過(guò)方法名和切入點(diǎn)進(jìn)行匹配的.
iii. 這個(gè)的方法名可以隨便起.
iv. Before方法同理
c. 帶Return的After方法,
@AfterReturning(returning = "str", pointcut = "pointcut()") public void doAfterReturning(String str) throws Exception { System.out.println("Return value is: " + str); }
i. AfterReturn是指在被切入的方法執(zhí)行后, 獲取其返回值, 再執(zhí)行該方法. 注意關(guān)鍵, 這個(gè)可以進(jìn)行操作返回值.
ii. returning = "str",是指, 假設(shè)切入方法的返回的值變量名為str
doAfterReturning(String str)方法的參數(shù)變量名必須和和returning保持一致, 這里也叫作str. 然后才能在方法體中使用.
iii. pointcut = "pointcut()"同樣是指前面聲明的pointcut方法名
3. 通過(guò)注解, 使用切入點(diǎn)
a. 監(jiān)聽(tīng)方法參數(shù)
@Before("execution(public int com.sguess.service.*(int, int))") public void beforMethod(JoinPoint point) { String methodName = point.getSignature().getName(); List<Object> args = Arrays.asList(point.getArgs()); System.out.println("Before FunctionName:" + methodName + ",ParameterName:" + args); } @After("execution(public int com.sguess.service.*(int, int))") public void afterMethod(JoinPoint point) { String methodName = point.getSignature().getName(); List<Object> args = Arrays.asList(point.getArgs()); System.out.println("After FunctionName:" + methodName + ",ParameterName:" + args); }
4. 執(zhí)行順序:
a.Around的方法優(yōu)先于Before/After執(zhí)行,After優(yōu)先于AfterReturn.
i. 代碼
@Before("execution(public int com.sguess.service.*.*(int, int))") public void beforMethod(JoinPoint point) { System.out.println("Before function"); } @After("execution(public int com.sguess.service.*.*(int, int))") public void afterMethod(JoinPoint point) { System.out.println("After function"); } @AfterReturning("execution(public int com.sguess.service.*.*(int, int))") public void afterReturnMethod(JoinPoint point) { System.out.println("AfterReturn function"); } @AfterThrowing(value = "execution(public int com.sguess.service.*.*(int, int))", throwing = "e") public void afterReturningThrowing(JoinPoint point, Exception e) { System.out.println("AfterReturnThrowing function"); } @Around("execution(public int com.sguess.service.*.*(int, int))") public Object aroundMethod(ProceedingJoinPoint pdj) { System.out.println("Start AroundFunction"); Object result = null; try { System.out.println("Around process start"); result = pdj.proceed(); System.out.println("Around process end"); } catch (Throwable e) { System.out.println("Around process exception"); } System.out.println("After Around process"); return result; } }
執(zhí)行結(jié)果:
Start AroundFunction
Around process start
Before function
Around process end
After Around process
After function
AfterReturn function
5.小結(jié):
@AfterReturning(returning = "str", pointcut = "pointcut()") public void doAfterReturning(String str) throws Exception { System.out.println("Return value is: " + str); } @Before("execution(public int com.sguess.service.*.*(int, int))") public void beforMethod(JoinPoint point) { String methodName = point.getSignature().getName(); List<Object> args = Arrays.asList(point.getArgs()); System.out.println("Before FunctionName:" + methodName + ",ParameterName:" + args); } @After("execution(public int com.sguess.service.*.*(int, int))") public void afterMethod(JoinPoint point) { String methodName = point.getSignature().getName(); List<Object> args = Arrays.asList(point.getArgs()); System.out.println("After FunctionName:" + methodName + ",ParameterName:" + args); } @AfterThrowing(value = "execution(public int com.sguess.service.*.*(int, int))", throwing = "e") public void afterReturningThrowing(JoinPoint point, Exception e) { String methodName = point.getSignature().getName(); List<Object> args = Arrays.asList(point.getArgs()); System.out.println("AfterReturningThrowing FunctionName:" + methodName + ",ParameterName:" + args + ",Exception:" + e); } @Around("execution(public int com.sguess.service.*.*(int, int))") public Object aroundMethod(ProceedingJoinPoint pdj) { System.out.println("Start AroundFunction"); Object result = null; try { System.out.println("Around process start"); result = pdj.proceed(); System.out.println("Around process end"); } catch (Throwable e) { System.out.println("Around process exception"); } System.out.println("After Around process"); return result; }
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
Opencv實(shí)現(xiàn)身份證OCR識(shí)別的示例詳解
這篇文章主要為大家詳細(xì)介紹了如何使用Opencv實(shí)現(xiàn)身份證OCR識(shí)別功能,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以跟隨小編一起了解一下2024-03-03StringUtils里的isEmpty方法和isBlank方法的區(qū)別詳解
這篇文章主要介紹了StringUtils里的isEmpty方法和isBlank方法的區(qū)別詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2020-04-04Java實(shí)現(xiàn)動(dòng)態(tài)獲取文件的絕對(duì)路徑
我們知道在?Java?中讀取一些配置文件信息,是在開(kāi)發(fā)中十分常用的要求。這篇文章就來(lái)和大家聊聊Java如何實(shí)現(xiàn)動(dòng)態(tài)獲取文件的絕對(duì)路徑,感興趣的可以了解一下2023-02-02Java請(qǐng)求流量合并和拆分提高系統(tǒng)的并發(fā)量示例
這篇文章主要為大家介紹了Java請(qǐng)求流量合并和拆分提高系統(tǒng)的并發(fā)量示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2022-04-04Java程序設(shè)計(jì)之12個(gè)經(jīng)典樣例
這篇文章主要給大家分享關(guān)于Java程序設(shè)計(jì)11個(gè)經(jīng)典樣例,主要以舉例的形式詳細(xì)的講解了Java程序設(shè)計(jì)的各種方法,需要的朋友可以參考一下文章具體的內(nèi)容2021-10-10Java開(kāi)發(fā)實(shí)現(xiàn)的Socket雙向通信功能示例
這篇文章主要介紹了Java開(kāi)發(fā)實(shí)現(xiàn)的Socket雙向通信功能,結(jié)合實(shí)例形式分析了java基于socket實(shí)現(xiàn)的服務(wù)器端與客戶端雙向通信相關(guān)操作技巧,需要的朋友可以參考下2018-01-01java基于雙向環(huán)形鏈表解決丟手帕問(wèn)題的方法示例
這篇文章主要介紹了java基于雙向環(huán)形鏈表解決丟手帕問(wèn)題的方法,簡(jiǎn)單描述了丟手帕問(wèn)題,并結(jié)合實(shí)例形式給出了Java基于雙向環(huán)形鏈表解決丟手帕問(wèn)題的步驟與相關(guān)操作技巧,需要的朋友可以參考下2017-11-11java隨機(jī)生成8位數(shù)授權(quán)碼的實(shí)例
下面小編就為大家?guī)?lái)一篇java隨機(jī)生成8位數(shù)授權(quán)碼的實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02