Spring注解驅動之AOP功能測試
前言
Spring的AOP指的是在程序運行期間動態的將某段代碼切入到指定方法指定位置進行運行的編程方式【動態代理】。
AOP功能測試
①導入AOP模塊
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>4.3.12.RELEASE</version> </dependency>
②定義邏輯組件和切面類
邏輯組件
在業務邏輯運行的時候將日志進行打印(方法之前、方法運行結束、方法出現異常,xxx)
public class MathCalculator { public int div(int i,int j){ System.out.println('MathCalculator...div...'); return i/j; }}
切面類
切面類里面的方法需要動態感知MathCalculator.div運行到哪里然后執行;
/** * 切面類 必須告訴Spring哪個類是切面類(給切面類上加一個注解:@Aspect) * @Aspect: 告訴Spring當前類是一個切面類 * */@Aspectpublic class LogAspects { //抽取公共的切入點表達式 //1、本類引用 pointCut() //2、其他的切面引用 com.atneusoft.springboot.aop.LogAspects.pointCut() @Pointcut('execution(public int com.atneusoft.springboot.aop.MathCalculator.*(..))') public void pointCut(){}; //@Before在目標方法之前切入;切入點表達式(指定在哪個方法切入) //給切面類的目標方法標注何時何地運行(通知注解@Before@After@AfterReturning@AfterThrowing) //前置通知(@Before):在目標方法(div)運行之前運行 @Before('pointCut()') public void logStart(JoinPoint joinPoint){ Object[] args = joinPoint.getArgs(); System.out.println(''+joinPoint.getSignature().getName()+'運行。。。@Before:參數列表是:{'+Arrays.asList(args)+'}'); } //后置通知(@After):在目標方法(div)運行結束之后運行(無論方法正常結束還是異常結束) @After('com.atneusoft.springboot.aop.LogAspects.pointCut()') public void logEnd(JoinPoint joinPoint){ System.out.println(''+joinPoint.getSignature().getName()+'結束。。。@After'); } //JoinPoint一定要出現在參數表的第一位 //返回通知(@AfterReturning):在目標方法(div)正常返回之后運行 @AfterReturning(value='pointCut()',returning='result') public void logReturn(JoinPoint joinPoint,Object result){ System.out.println(''+joinPoint.getSignature().getName()+'正常返回。。。@AfterReturning:運行結果:{'+result+'}'); } //異常通知(@AfterThrowing):在目標方法(div)出現異常以后運行 @AfterThrowing(value='pointCut()',throwing='exception') public void logException(JoinPoint joinPoint,Exception exception){ System.out.println(''+joinPoint.getSignature().getName()+'異常。。。異常信息:{'+exception+'}'); }}
③將切面類和業務邏輯類(目標方法所在類)都加入到容器中,給配置類中加 @EnableAspectJAutoProxy 【開啟基于注解的aop模式,與配置文件的以下形式相同
<!-- 開啟基于注解版的切面功能 --><aop:aspectj-autoproxy></aop:aspectj-autoproxy>
@EnableAspectJAutoProxy@Configurationpublic class MainConfigOfAOP { //業務邏輯類加入容器中 @Bean public MathCalculator calculator(){ return new MathCalculator(); } //切面類加入到容器中 @Bean public LogAspects logAspects(){ return new LogAspects(); }}
@Test public void test01(){ AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfAOP.class);//1、不要自己創建對象// MathCalculator mathCalculator = new MathCalculator();// mathCalculator.div(1, 1); MathCalculator mathCalculator = applicationContext.getBean(MathCalculator.class);mathCalculator.div(1, 0);applicationContext.close(); }
07:49:45.185 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean ’mathCalculator’div運行。。。@Before:參數列表是:{[1, 1]}MathCalculator...div...div結束。。。@Afterdiv正常返回。。。@AfterReturning:運行結果:{1}com.atneusoft.springboot.aop.MathCalculator@5965be2d
總結
三步:
1)、將業務邏輯組件和切面類都加入到容器中;告訴Spring哪個是切面類(@Aspect)
2)、在切面類上的每一個通知方法上標注通知注解,告訴Spring何時何地運行(切入點表達式)
3)、開啟基于注解的aop模式;@EnableAspectJAutoProxy
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章:
1. React+umi+typeScript創建項目的過程2. .Net core 的熱插拔機制的深入探索及卸載問題求救指南3. ASP調用WebService轉化成JSON數據,附json.min.asp4. SharePoint Server 2019新特性介紹5. 三個不常見的 HTML5 實用新特性簡介6. 解決ASP中http狀態跳轉返回錯誤頁的問題7. ASP中常用的22個FSO文件操作函數整理8. 無線標記語言(WML)基礎之WMLScript 基礎第1/2頁9. ASP.NET Core 5.0中的Host.CreateDefaultBuilder執行過程解析10. ASP編碼必備的8條原則
