Spring AOP使用@Aspect注解 面向切面實現(xiàn)日志橫切的操作
引言:
AOP為Aspect Oriented Programming的縮寫,意為:面向切面編程,通過預編譯方式和運行期動態(tài)代理實現(xiàn)程序功能的統(tǒng)一維護的一種技術(shù).AOP是OOP的延續(xù),是軟件開發(fā)中的一個熱點,也是Spring框架中的一個重要內(nèi)容,是函數(shù)式編程的一種衍生范型。
利用AOP可以對業(yè)務邏輯的各個部分進行隔離,從而使得業(yè)務邏輯各部分之間的耦合度降低,提高程序的可重用性,同時提高了開發(fā)的效率。
在Spring AOP中業(yè)務邏輯僅僅只關(guān)注業(yè)務本身,將日志記錄,性能統(tǒng)計,安全控制,事務處理,異常處理等代碼從業(yè)務邏輯代碼中劃分出來,通過對這些行為的分離,我們希望可以將它們獨立到非指導業(yè)務邏輯的方法中,進而改變這些行為的時候不影響業(yè)務邏輯的代碼。
相關(guān)注解介紹如下:
@Aspect
:作用是把當前類標識為一個切面供容器讀取
@Pointcut
:Pointcut是植入Advice的觸發(fā)條件。每個Pointcut的定義包括2部分,一是表達式,二是方法簽名。方法簽名必須是 public及void型??梢詫ointcut中的方法看作是一個被Advice引用的助記符,因為表達式不直觀,因此我們可以通過方法簽名的方式為 此表達式命名。因此Pointcut中的方法只需要方法簽名,而不需要在方法體內(nèi)編寫實際代碼。
@Around
:環(huán)繞增強,相當于MethodInterceptor
@AfterReturning
:后置增強,相當于AfterReturningAdvice,方法正常退出時執(zhí)行
@Before
:標識一個前置增強方法,相當于BeforeAdvice的功能,相似功能的還有
@AfterThrowing
:異常拋出增強,相當于ThrowsAdvice
一:引入相關(guān)依賴
二:Spring的配置文件
applicationContext.xml 中引入context、aop對應的命名空間;配置自動掃描的包,同時使切面類中相關(guān)方法中的注解生效,需自動地為匹配到的方法所在的類生成代理對象。
創(chuàng)建簡單計算器的接口ArithmeticCalculator.java及實現(xiàn)類ArithmeticCalculatorImpl.java
package com.svse.aop; public interface ArithmeticCalculator { //定義四個簡單的借口 加減乘除算法 int add(int i, int j); int sub(int i, int j); int mul(int i, int j); int div(int i, int j); }
package com.svse.aop; import org.springframework.stereotype.Component; //將實現(xiàn)類加入Spring的IOC容器進行管理 @Component("arithmeticCalculator") public class ArithmeticCalculatorImpl implements ArithmeticCalculator { @Override public int add(int i, int j) { int result = i + j; return result; } @Override public int sub(int i, int j) { int result = i - j; return result; } @Override public int mul(int i, int j) { int result = i * j; return result; } @Override public int div(int i, int j) { int result = i / j; return result; } }
現(xiàn)在想在實現(xiàn)類中的每個方法執(zhí)行前、后、以及是否發(fā)生異常等信息打印出來,需要把日志信息抽取出來,寫到對應的切面的類中 LoggingAspect.java 中 要想把一個類變成切面類,需要兩步,
① 在類上使用 @Component 注解 把切面類加入到IOC容器中
② 在類上使用 @Aspect 注解 使之成為切面類
package com.svse.aop; 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; import com.tencentcloudapi.vod.v20180717.VodClient; /** * 日志切面 * * @author zhaoshuiqing<br> * @date 2019年6月14日 下午3:03:29 */ @Component @Aspect public class LoggingAspect { //現(xiàn)在想在實現(xiàn)類中的每個方法執(zhí)行前、后、以及是否發(fā)生異常等信息打印出來,需要把日志信息抽取出來,寫到對應的切面的類中 LoggingAspect.java 中 //要想把一個類變成切面類,需要兩步, //① 在類上使用 @Component 注解 把切面類加入到IOC容器中 //② 在類上使用 @Aspect 注解 使之成為切面類 /** * 前置通知:目標方法執(zhí)行之前執(zhí)行以下方法體的內(nèi)容 * @param jp */ @Before("execution(* com.svse.aop.*.*(..))") public void beforeMethod(JoinPoint jp){ String methodName =jp.getSignature().getName(); System.out.println("【前置通知】the method 【" + methodName + "】 begins with " + Arrays.asList(jp.getArgs())); } /** * 返回通知:目標方法正常執(zhí)行完畢時執(zhí)行以下代碼 * @param jp * @param result */ @AfterReturning(value="execution(* com.svse.aop.*.*(..))",returning="result") public void afterReturningMethod(JoinPoint jp, Object result){ String methodName =jp.getSignature().getName(); System.out.println("【返回通知】the method 【" + methodName + "】 ends with 【" + result + "】"); } /** * 后置通知:目標方法執(zhí)行之后執(zhí)行以下方法體的內(nèi)容,不管是否發(fā)生異常。 * @param jp */ @After("execution(* com.svse.aop.*.*(..))") public void afterMethod(JoinPoint jp){ System.out.println("【后置通知】this is a afterMethod advice..."); } /** * 異常通知:目標方法發(fā)生異常的時候執(zhí)行以下代碼 */ @AfterThrowing(value="execution(* com.qcc.beans.aop.*.*(..))",throwing="e") public void afterThorwingMethod(JoinPoint jp, NullPointerException e){ String methodName = jp.getSignature().getName(); System.out.println("【異常通知】the method 【" + methodName + "】 occurs exception: " + e); } /** * 環(huán)繞通知:目標方法執(zhí)行前后分別執(zhí)行一些代碼,發(fā)生異常的時候執(zhí)行另外一些代碼 * @return */ /*@Around(value="execution(* com.svse.aop.*.*(..))") public Object aroundMethod(ProceedingJoinPoint jp){ String methodName = jp.getSignature().getName(); Object result = null; try { System.out.println("【環(huán)繞通知中的--->前置通知】:the method 【" + methodName + "】 begins with " + Arrays.asList(jp.getArgs())); //執(zhí)行目標方法 result = jp.proceed(); System.out.println("【環(huán)繞通知中的--->返回通知】:the method 【" + methodName + "】 ends with " + result); } catch (Throwable e) { System.out.println("【環(huán)繞通知中的--->異常通知】:the method 【" + methodName + "】 occurs exception " + e); } System.out.println("【環(huán)繞通知中的--->后置通知】:-----------------end.----------------------"); return result; }*/ }
編寫MainTest方法進行測試
package com.svse.aop; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainTest { public static void main(String[] args) { //ClassPathXmlApplicationContext默認是加載src目錄下的xml文件 ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml"); ArithmeticCalculator arithmeticCalculator =(ArithmeticCalculator) ctx.getBean("arithmeticCalculator"); System.out.println(arithmeticCalculator.getClass()); int result = arithmeticCalculator.add(3, 5); System.out.println("result: " + result); result = arithmeticCalculator.div(5, 0); System.out.println("result: " + result); } }
運行結(jié)果:
把其它代碼都注釋掉,把環(huán)繞通知的方法釋放出來,測試結(jié)果如下:
至此AOP注解面向切面記錄日志就寫完了!
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
mybatis中映射文件(mapper)中的使用規(guī)則
這篇文章主要介紹了mybatis中映射文件(mapper)中的使用規(guī)則,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11springboot多項目結(jié)構(gòu)實現(xiàn)
本文主要介紹了springboot多項目結(jié)構(gòu)實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-01-01springboot中nacos-client獲取配置的實現(xiàn)方法
本文主要介紹了springboot中nacos-client獲取配置的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-04-04HashMap vs TreeMap vs Hashtable vs LinkedHashMap
這篇文章主要介紹了HashMap vs TreeMap vs Hashtable vs LinkedHashMap的相關(guān)知識,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2019-07-07關(guān)于break和continue以及l(fā)abel的區(qū)別和作用(詳解)
下面小編就為大家?guī)硪黄P(guān)于break和continue以及l(fā)abel的區(qū)別和作用(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05Nebula?Graph介紹和SpringBoot環(huán)境連接和查詢操作
Nebula?Graph?是一款開源的、分布式的、易擴展的原生圖數(shù)據(jù)庫,能夠承載包含數(shù)千億個點和數(shù)萬億條邊的超大規(guī)模數(shù)據(jù)集,并且提供毫秒級查詢,這篇文章主要介紹了Nebula?Graph介紹和SpringBoot環(huán)境連接和查詢,需要的朋友可以參考下2022-10-10