spring項目中切面及AOP的使用方法
使用AOP的原因(AOP簡介)
我們知道,spring兩大核心,IOC(控制反轉(zhuǎn))和AOP(切面),那為什么要使用AOP,AOP是什么呢,嚴(yán)格來說,AOP是一種編程規(guī)范,是一種編程思想,并非spring創(chuàng)造,AOP可以幫助我們在一定程度上從冗余的通用的業(yè)務(wù)邏輯中解脫出來,最明顯的,比如每個接口的請求,都要記錄日志,那這個操作如果每個地方都寫,就會很繁瑣,當(dāng)然,記錄日志并不是唯一的用法
spring的AOP只能基于IOC來管理,它只能作用于spring容器的bean
并且,spring的AOP為的是解決企業(yè)開發(fā)中出現(xiàn)最普遍的方法織入,并不是為了像AspectJ那樣,成為一個完全的AOP使用解決方案
AOP的使用
開啟AOP支持
要使用AOP,首先要開啟AOP的支持
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
啟動類添加 @EnableAspectJAutoProxy 注解
編寫切面類與測試方法
@Aspect @Component public class MyAop { }
@RestController public 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
這個注解的用法呢,就是說,在執(zhí)行你要執(zhí)行的東西之前,執(zhí)行加了這個注解的方法
比如
@Before(value = "execution (* own.study.web.OneController.*(..))") public void doAop( ) { System.out.println("before aop"); }
也就是說,如果我要調(diào)用 OneController 的方法,在調(diào)用到之前,會執(zhí)行這個 doAop 方法
讓我們來測試一下
@After
這個注解的用法,就是說,當(dāng)你執(zhí)行完你的方法之后,真的返回給調(diào)用方之前,執(zhí)行加了這個注解的方法
比如
@After(value = "execution (* own.study.web.OneController.*(..))") public void doAfter() { System.out.println("after aop"); }
讓我們來測試一下
@AfterThrowing
見名知意,在發(fā)生異常后,執(zhí)行加了此注解的方法
注意我上面寫的測試方法了嗎?我拋出了自定義的異常
讓我們測試一下
@AfterReturning
這個注解的用法也是看名字就能猜到,執(zhí)行完后,執(zhí)行此方法
但是!這個執(zhí)行完,指的是正常執(zhí)行完,不拋出異常的那種,不信?我們來試試
@Around
這個是最為強(qiáng)大的一個注解,環(huán)繞通知,方法執(zhí)行前和執(zhí)行后都會執(zhí)行加了這個注解的方法
@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; }
@RestController public class OneController { @GetMapping("/doCheck") public Object doCheck (int age) throws InterruptedException { System.out.println("這個是controller的方法 --->" + System.currentTimeMillis()); Thread.sleep(2000l); System.out.println("doCheck"); return new MyRsp("1", "success"); } }
但是,注意!這個環(huán)繞通知不是萬能的,不是一定好,大家按需要使用,比如一個場景,當(dāng)你的方法拋出了異常,這個環(huán)繞通知就不會再繼續(xù)執(zhí)行
我們來實驗一下
改寫controller的方法
@RestController public class OneController { @GetMapping("/doCheck") public Object doCheck (int age) throws InterruptedException { System.out.println("這個是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的使用的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
jstack報錯Unable to open socket file解決
這篇文章主要為大家介紹了jstack報錯Unable to open socket file的解決方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-02-02Java使用DateFormatter格式化日期時間的方法示例
這篇文章主要介紹了Java使用DateFormatter格式化日期時間的方法,結(jié)合具體實例分析了java使用DateFormatter格式化日期時間的相關(guān)操作技巧,需要的朋友可以參考下2017-04-04SpringBoot整合Swagger Api自動生成文檔的實現(xiàn)
本文主要介紹了SpringBoot整合Swagger Api自動生成文檔的實,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06Rxjava+Retrofit+MVP實現(xiàn)購物車功能
這篇文章主要為大家詳細(xì)介紹了Rxjava+Retrofit+MVP實現(xiàn)購物車功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05Springboot整合Spring Cloud Kubernetes讀取ConfigMap支持自動刷新配置的教程
這篇文章主要介紹了Springboot整合Spring Cloud Kubernetes讀取ConfigMap支持自動刷新配置,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09