日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring(十九):Spring AOP(三):切面的优先级、重复使用切入点表达式

發布時間:2023/12/18 javascript 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring(十九):Spring AOP(三):切面的优先级、重复使用切入点表达式 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

背景:

1)指定切面優先級示例:有的時候需要對一個方法指定多個切面,而這多個切面有時又需要按照不同順序執行,因此,切面執行優先級別指定功能就變得很實用。

2)重復使用切入點表達式:上一篇文章中,定義前置、后置、返回、異常通知的切入點表達式時,都使用了同一個;而且本章節新加入的驗證切面ValidateAspect類,也使用同一個切入點表達式,怎么讓他們重用呢?

指定切面優先級示例:

比如在算術計算器執行計算之前進行數據驗證,驗證完事之后才讓執行日志輸出。

新建spring aop項目參考:《Spring(十八):Spring AOP(二):通知(前置、后置、返回、異常、環繞)》

添加驗證切面類:

package com.dx.spring.beans.aop;import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component;@Component @Aspect public class ValidateAspect { @Before("execution(public int com.dx.spring.beans.aop.IArithmeticCalculator.*(..))")public void beforeMethod() {System.out.println("validate...");} }

除了驗證切面,還包含日志切面:

package com.dx.spring.beans.aop;import java.util.List; import java.util.Arrays;import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component;//把這個類聲明為一個切面:需要把該類放入到IOC容器中、再聲明為一個切面。 @Aspect @Component public class LoggingAspect {// 聲明該方法為一個前置通知:在目標方法開始之前執行@Before("execution(public int com.dx.spring.beans.aop.IArithmeticCalculator.*(int, int))")public void beforeMethod(JoinPoint joinpoint) {String methodName = joinpoint.getSignature().getName();List<Object> args = Arrays.asList(joinpoint.getArgs());System.out.println("before method " + methodName + " with " + args);}// 聲明該方法為一個后置通知:在目標方法結束之后執行(無論方法是否拋出異常)。// 但是因為當方法拋出異常時,不能返回值為null,因此這里無法獲取到異常值。@After(value = "execution(public int com.dx.spring.beans.aop.IArithmeticCalculator.*(int,int))")public void afterMethod(JoinPoint joinpoint) {String methodName = joinpoint.getSignature().getName();List<Object> args = Arrays.asList(joinpoint.getArgs());System.out.println("after method " + methodName);}/*** 聲明該方法為一個返回通知:在目標方法返回結果時后執行(當方法拋出異常時,無法執行)。 但是因為當方法拋出異常時,類似代碼 {@code* try <br/>* { <br/>* // before 前置通知 <br/>* // do action method <br/>* // after returning 返回通知,可以訪問到方法的返回值 <br/>* } catch(Exception e){ e.printStackTrace(); // after throwing* 異常通知,可以訪問到方法出現的異常 } // after 后置通知,因為方法可以拋出異常,所以訪問不到方法的返回值 }*/@AfterReturning(value = "execution(public int com.dx.spring.beans.aop.IArithmeticCalculator.*(int,int))", returning = "result")public void afterReturningMethod(JoinPoint joinpoint, Object result) {String methodName = joinpoint.getSignature().getName();List<Object> args = Arrays.asList(joinpoint.getArgs());System.out.println("after method " + methodName + " with returning " + (result == null ? "NULL" : result.toString()));}/*** 定義一個異常通知函數: 只有在方法跑吹異常時,該方法才會執行,而且可以接受異常對象。*/@AfterThrowing(value = "execution(public int com.dx.spring.beans.aop.IArithmeticCalculator.*(int,int))", throwing = "ex")public void afterThrowingMethod(JoinPoint joinpoint, Exception ex) {String methodName = joinpoint.getSignature().getName();List<Object> args = Arrays.asList(joinpoint.getArgs());System.out.println("after method " + methodName + " occurs exception: " + (ex == null ? "NULL" : ex.getMessage()));}// @Around(value = "execution(public int com.dx.spring.beans.aop.IArithmeticCalculator.*(int, int))") // public Object aroundMethod(ProceedingJoinPoint pJoinPoint) throws Exception { // String methodName = pJoinPoint.getSignature().getName(); // List<Object> args = Arrays.asList(pJoinPoint.getArgs()); // // Object result = null; // try { // // 前置通知 // System.out.println("before method " + methodName + " with " + args); // // 執行目標方法 // result = pJoinPoint.proceed(); // // 返回通知 // System.out.println("after method " + methodName + " returning " + result); // } catch (Throwable ex) { // // 異常通知 // System.out.println("after method " + methodName + " occurs exception: " + ex.getMessage()); // throw new Exception(ex); // } // // 后置通知 // System.out.println("after method " + methodName); // return result; // } }

默認打印結果:

按照實際業務需求來講,更希望是先執行驗證切面,才執行日志切面,那么,可以通過設置切面優先級別來設置:

修改ValidateAspect.java添加注解@Order(1)

@Order(value=1) @Component @Aspect public class ValidateAspect { @Before("execution(public int com.dx.spring.beans.aop.IArithmeticCalculator.*(..))")public void beforeMethod() {System.out.println("validate...");} }

修改LoggingAspect.java添加注解@Order(2)

@Order(value=2) @Aspect @Component public class LoggingAspect {//代碼 }

此時執行結果為:

重使用切入點表達式:

上一篇文章中,定義前置、后置、返回、異常通知的切入點表達式時,都使用了同一個;而且本章節新加入的驗證切面ValidateAspect類,也使用同一個切入點表達式,怎么讓他們重用呢?

第一步:修改LoggingAspect切面類,添加@Pointcut注解的方法declareAspectJoinPointExpression():

//把這個類聲明為一個切面:需要把該類放入到IOC容器中、再聲明為一個切面。 @Order(value=2) @Aspect @Component public class LoggingAspect {@Pointcut("execution(public int com.dx.spring.beans.aop.IArithmeticCalculator.*(..))")public void declareAspectJoinPointExpression(){}//... }

第二步:修改LoggingAspect切面類,把前置、后置、返回、異常、環繞通知方法的切入點表達式替換為“declareAspectJoinPointExpression()”

以前置通知方法為例:

//把這個類聲明為一個切面:需要把該類放入到IOC容器中、再聲明為一個切面。 @Order(value=2) @Aspect @Component public class LoggingAspect {@Pointcut("execution(public int com.dx.spring.beans.aop.IArithmeticCalculator.*(..))")public void declareAspectJoinPointExpression(){}// 聲明該方法為一個前置通知:在目標方法開始之前執行@Before("declareAspectJoinPointExpression()")public void beforeMethod(JoinPoint joinpoint) {String methodName = joinpoint.getSignature().getName();List<Object> args = Arrays.asList(joinpoint.getArgs());System.out.println("before method " + methodName + " with " + args);}//... }

第三步:修改驗證切面類ValidateAspect.java:

@Order(value=1) @Component @Aspect public class ValidateAspect { @Before("com.dx.spring.beans.aop.LoggingAspect.declareAspectJoinPointExpression()")public void beforeMethod() {System.out.println("validate...");} }

備注:如果在同一個包下,可以不指定包的路徑。

第四步:驗證

?

轉載于:https://www.cnblogs.com/yy3b2007com/p/9131005.html

總結

以上是生活随笔為你收集整理的Spring(十九):Spring AOP(三):切面的优先级、重复使用切入点表达式的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。