国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

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

spring項(xiàng)目中切面及AOP的使用方法

瀏覽:6日期:2023-07-06 08:25:17
使用AOP的原因(AOP簡(jiǎn)介)

我們知道,spring兩大核心,IOC(控制反轉(zhuǎn))和AOP(切面),那為什么要使用AOP,AOP是什么呢,嚴(yán)格來(lái)說(shuō),AOP是一種編程規(guī)范,是一種編程思想,并非spring創(chuàng)造,AOP可以幫助我們?cè)谝欢ǔ潭壬蠌娜哂嗟耐ㄓ玫臉I(yè)務(wù)邏輯中解脫出來(lái),最明顯的,比如每個(gè)接口的請(qǐng)求,都要記錄日志,那這個(gè)操作如果每個(gè)地方都寫,就會(huì)很繁瑣,當(dāng)然,記錄日志并不是唯一的用法

spring的AOP只能基于IOC來(lái)管理,它只能作用于spring容器的bean

并且,spring的AOP為的是解決企業(yè)開發(fā)中出現(xiàn)最普遍的方法織入,并不是為了像AspectJ那樣,成為一個(gè)完全的AOP使用解決方案

AOP的使用

開啟AOP支持

要使用AOP,首先要開啟AOP的支持

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId></dependency>

啟動(dòng)類添加 @EnableAspectJAutoProxy 注解

編寫切面類與測(cè)試方法

@Aspect@Componentpublic class MyAop { }

@RestControllerpublic class OneController { @GetMapping('/doCheck') public String doCheck (int age) {System.out.println('doCheck');if (age > 1) {throw new MyException(ExceptionEnu.SUCCESS);} else { throw new MyException(ExceptionEnu.FAILD);} } }

記得切面類交給spring管理哦~ @Component

編寫切面方法

@Before

這個(gè)注解的用法呢,就是說(shuō),在執(zhí)行你要執(zhí)行的東西之前,執(zhí)行加了這個(gè)注解的方法

比如

@Before(value = 'execution (* own.study.web.OneController.*(..))') public void doAop( ) {System.out.println('before aop'); }

也就是說(shuō),如果我要調(diào)用 OneController 的方法,在調(diào)用到之前,會(huì)執(zhí)行這個(gè) doAop 方法

讓我們來(lái)測(cè)試一下

spring項(xiàng)目中切面及AOP的使用方法

@After

這個(gè)注解的用法,就是說(shuō),當(dāng)你執(zhí)行完你的方法之后,真的返回給調(diào)用方之前,執(zhí)行加了這個(gè)注解的方法

比如

@After(value = 'execution (* own.study.web.OneController.*(..))') public void doAfter() {System.out.println('after aop'); }

讓我們來(lái)測(cè)試一下

spring項(xiàng)目中切面及AOP的使用方法

@AfterThrowing

見名知意,在發(fā)生異常后,執(zhí)行加了此注解的方法

注意我上面寫的測(cè)試方法了嗎?我拋出了自定義的異常

讓我們測(cè)試一下

spring項(xiàng)目中切面及AOP的使用方法

@AfterReturning

這個(gè)注解的用法也是看名字就能猜到,執(zhí)行完后,執(zhí)行此方法

但是!這個(gè)執(zhí)行完,指的是正常執(zhí)行完,不拋出異常的那種,不信?我們來(lái)試試

spring項(xiàng)目中切面及AOP的使用方法

@Around

這個(gè)是最為強(qiáng)大的一個(gè)注解,環(huán)繞通知,方法執(zhí)行前和執(zhí)行后都會(huì)執(zhí)行加了這個(gè)注解的方法

@Around(value = 'execution (* own.study.web.OneController.*(..))') public Object doAround (ProceedingJoinPoint point) throws Throwable {Gson gson = new Gson();System.out.println('進(jìn)入AOP --->' + System.currentTimeMillis());System.out.println('方法名 = ' + point.getSignature().toShortString()); Object result = point.proceed(); System.out.println('響應(yīng)參數(shù)為 = ' + gson.toJson(result));System.out.println('AOP完事了 --->' + System.currentTimeMillis());return result; }

@RestControllerpublic class OneController { @GetMapping('/doCheck') public Object doCheck (int age) throws InterruptedException {System.out.println('這個(gè)是controller的方法 --->' + System.currentTimeMillis());Thread.sleep(2000l);System.out.println('doCheck');return new MyRsp('1', 'success'); } }

spring項(xiàng)目中切面及AOP的使用方法

但是,注意!這個(gè)環(huán)繞通知不是萬(wàn)能的,不是一定好,大家按需要使用,比如一個(gè)場(chǎng)景,當(dāng)你的方法拋出了異常,這個(gè)環(huán)繞通知就不會(huì)再繼續(xù)執(zhí)行

我們來(lái)實(shí)驗(yàn)一下

改寫controller的方法

@RestControllerpublic class OneController { @GetMapping('/doCheck') public Object doCheck (int age) throws InterruptedException {System.out.println('這個(gè)是controller的方法 --->' + System.currentTimeMillis());Thread.sleep(2000l);System.out.println('doCheck');throw new MyException('1', 'success'); // return new MyRsp('1', 'success'); } }

spring項(xiàng)目中切面及AOP的使用方法

看,AOP后續(xù)的沒(méi)有被執(zhí)行

以上就是spring的切面,AOP的使用的詳細(xì)內(nèi)容,更多關(guān)于spring的切面,AOP的使用的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 日韩在线观看中文字幕 | 欧美一级v片 | 99爱视频 | 日本人成在线视频免费播放 | 欧美三级一区二区 | 久久精品免费视频观看 | 正在播放国产乱子伦视频 | 久久久久网站 | 亚洲精品二区中文字幕 | 日本久久精品视频 | a一级免费| 欧美白人猛性xxxxx交69 | 久久香蕉国产线看观看精品yw | 中文字幕在线播放视频 | 暖暖日本在线播放 | 在线欧美视频 | 一级毛片免费不卡在线 | 欧美三级超在线视频 | 国产婷婷一区二区在线观看 | 岛国毛片在线观看 | 午夜爽爽| 国产免费高清在线精品一区 | 美女在线看永久免费网址 | 国产欧美va欧美va香蕉在线观 | 91玖玖 | 色综合久久88中文字幕 | 日韩一级精品视频在线观看 | 中美日韩在线网免费毛片视频 | 99久久精品国产一区二区 | 久久久久久综合成人精品 | 久久精品国产精品亚洲毛片 | 日本三级11k影院在线 | 成人在线精品视频 | 亚洲国产成人在线观看 | 成人黄色毛片 | 91精品一区二区综合在线 | 丝袜紧身裙国产在线播放 | 国产区网址 | 欧美国产一区二区三区 | 91精品视频播放 | 搞黄网站免费看 |