久久福利_99r_国产日韩在线视频_直接看av的网站_中文欧美日韩_久久一

您的位置:首頁技術(shù)文章
文章詳情頁

Spring注解驅(qū)動(dòng)之AOP功能測試

瀏覽:2日期:2023-09-10 17:12:59

前言

Spring的AOP指的是在程序運(yùn)行期間動(dòng)態(tài)的將某段代碼切入到指定方法指定位置進(jìn)行運(yùn)行的編程方式【動(dòng)態(tài)代理】。

AOP功能測試

①導(dǎo)入AOP模塊

<dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>4.3.12.RELEASE</version> </dependency>

Spring注解驅(qū)動(dòng)之AOP功能測試

②定義邏輯組件和切面類

邏輯組件

在業(yè)務(wù)邏輯運(yùn)行的時(shí)候?qū)⑷罩具M(jìn)行打印(方法之前、方法運(yùn)行結(jié)束、方法出現(xiàn)異常,xxx)

public class MathCalculator { public int div(int i,int j){ System.out.println('MathCalculator...div...'); return i/j; }}

切面類

切面類里面的方法需要?jiǎng)討B(tài)感知MathCalculator.div運(yùn)行到哪里然后執(zhí)行;

/** * 切面類 必須告訴Spring哪個(gè)類是切面類(給切面類上加一個(gè)注解:@Aspect) * @Aspect: 告訴Spring當(dāng)前類是一個(gè)切面類 * */@Aspectpublic class LogAspects { //抽取公共的切入點(diǎn)表達(dá)式 //1、本類引用 pointCut() //2、其他的切面引用 com.atneusoft.springboot.aop.LogAspects.pointCut() @Pointcut('execution(public int com.atneusoft.springboot.aop.MathCalculator.*(..))') public void pointCut(){}; //@Before在目標(biāo)方法之前切入;切入點(diǎn)表達(dá)式(指定在哪個(gè)方法切入) //給切面類的目標(biāo)方法標(biāo)注何時(shí)何地運(yùn)行(通知注解@Before@After@AfterReturning@AfterThrowing) //前置通知(@Before):在目標(biāo)方法(div)運(yùn)行之前運(yùn)行 @Before('pointCut()') public void logStart(JoinPoint joinPoint){ Object[] args = joinPoint.getArgs(); System.out.println(''+joinPoint.getSignature().getName()+'運(yùn)行。。。@Before:參數(shù)列表是:{'+Arrays.asList(args)+'}'); } //后置通知(@After):在目標(biāo)方法(div)運(yùn)行結(jié)束之后運(yùn)行(無論方法正常結(jié)束還是異常結(jié)束) @After('com.atneusoft.springboot.aop.LogAspects.pointCut()') public void logEnd(JoinPoint joinPoint){ System.out.println(''+joinPoint.getSignature().getName()+'結(jié)束。。。@After'); } //JoinPoint一定要出現(xiàn)在參數(shù)表的第一位 //返回通知(@AfterReturning):在目標(biāo)方法(div)正常返回之后運(yùn)行 @AfterReturning(value='pointCut()',returning='result') public void logReturn(JoinPoint joinPoint,Object result){ System.out.println(''+joinPoint.getSignature().getName()+'正常返回。。。@AfterReturning:運(yùn)行結(jié)果:{'+result+'}'); } //異常通知(@AfterThrowing):在目標(biāo)方法(div)出現(xiàn)異常以后運(yùn)行 @AfterThrowing(value='pointCut()',throwing='exception') public void logException(JoinPoint joinPoint,Exception exception){ System.out.println(''+joinPoint.getSignature().getName()+'異常。。。異常信息:{'+exception+'}'); }}

③將切面類和業(yè)務(wù)邏輯類(目標(biāo)方法所在類)都加入到容器中,給配置類中加 @EnableAspectJAutoProxy 【開啟基于注解的aop模式,與配置文件的以下形式相同

<!-- 開啟基于注解版的切面功能 --><aop:aspectj-autoproxy></aop:aspectj-autoproxy>

@EnableAspectJAutoProxy@Configurationpublic class MainConfigOfAOP { //業(yè)務(wù)邏輯類加入容器中 @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、不要自己創(chuàng)建對象// 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運(yùn)行。。。@Before:參數(shù)列表是:{[1, 1]}MathCalculator...div...div結(jié)束。。。@Afterdiv正常返回。。。@AfterReturning:運(yùn)行結(jié)果:{1}com.atneusoft.springboot.aop.MathCalculator@5965be2d

總結(jié)

三步:

1)、將業(yè)務(wù)邏輯組件和切面類都加入到容器中;告訴Spring哪個(gè)是切面類(@Aspect)

2)、在切面類上的每一個(gè)通知方法上標(biāo)注通知注解,告訴Spring何時(shí)何地運(yùn)行(切入點(diǎn)表達(dá)式)

3)、開啟基于注解的aop模式;@EnableAspectJAutoProxy

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 999在线视频免费观看 | 日韩中文字幕在线视频 | 日韩在线免费 | 久久精品日韩 | 亚洲一区欧美一区 | 最新免费av网站 | 久久久久久免费视频 | 一区二区三区四区在线 | 黄网站涩免费蜜桃网站 | 国产亚洲欧美在线 | 自拍视频免费 | 一级一片在线观看 | 亚洲精品在线播放 | 一级全黄性色生活片 | 国产精品久久久久久网站 | 北条麻妃一区二区三区在线观看 | 99国产精品久久久 | 亚洲免费在线视频 | 国产一级毛片在线视频 | 一区二区色 | 毛片一区二区三区 | 国产一区二区三区四区在线观看 | 夫妻午夜影院 | 精国产品一区二区三区四季综 | 国产不卡免费视频 | 亚洲国产精品久久久久秋霞蜜臀 | 色www精品视频在线观看 | 福利精品在线观看 | 人人爽在线观看 | 日韩精品小视频 | 久久777| 337p日本粉嫩噜噜噜 | 国产a视频| a视频在线观看免费 | 91免费在线看 | 成人亚洲精品久久久久 | 国产99久久久精品视频 | 国产四区 | 91午夜伦伦电影理论片 | 91在线观看 | 亚洲www啪成人一区二区 |