spring項(xiàng)目中切面及AOP的使用方法
我們知道,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è)試一下
@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è)試一下
@AfterThrowing
見名知意,在發(fā)生異常后,執(zhí)行加了此注解的方法
注意我上面寫的測(cè)試方法了嗎?我拋出了自定義的異常
讓我們測(cè)試一下
@AfterReturning
這個(gè)注解的用法也是看名字就能猜到,執(zhí)行完后,執(zhí)行此方法
但是!這個(gè)執(zhí)行完,指的是正常執(zhí)行完,不拋出異常的那種,不信?我們來(lái)試試
@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'); } }
但是,注意!這個(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'); } }
看,AOP后續(xù)的沒有被執(zhí)行
以上就是spring的切面,AOP的使用的詳細(xì)內(nèi)容,更多關(guān)于spring的切面,AOP的使用的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. html中的form不提交(排除)某些input 原創(chuàng)2. ASP常用日期格式化函數(shù) FormatDate()3. 開發(fā)效率翻倍的Web API使用技巧4. XMLHTTP資料5. CSS3實(shí)現(xiàn)動(dòng)態(tài)翻牌效果 仿百度貼吧3D翻牌一次動(dòng)畫特效6. asp.net core項(xiàng)目授權(quán)流程詳解7. vue使用moment如何將時(shí)間戳轉(zhuǎn)為標(biāo)準(zhǔn)日期時(shí)間格式8. CSS3中Transition屬性詳解以及示例分享9. jsp文件下載功能實(shí)現(xiàn)代碼10. ASP動(dòng)態(tài)網(wǎng)頁(yè)制作技術(shù)經(jīng)驗(yàn)分享
