SpringBoot基于自定義注解實現(xiàn)切面編程
更新時間:2020年11月23日 08:52:36 作者:_不正
這篇文章主要介紹了SpringBoot基于自定義注解實現(xiàn)切面編程,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
1、相關(guān)依賴包
<!-- aop 依賴包 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.8.6</version> </dependency>
2、定義切面類
package com.bz.aspect; import com.bz.service.SysLogOperationService; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; /** * 操作日志,切面處理類 */ @Aspect @Component public class LogOperationAspect { @Autowired(required = false) private SysLogOperationService sysLogOperationService; @Pointcut("@annotation(com.bz.aspect.BzLogOperation)") public void logPointCut() { System.out.println("lllll"); } /** * 前置通知:方法執(zhí)行前調(diào)用 */ @Before("logPointCut()") public void begin() { System.out.println("前置通知:方法執(zhí)行前調(diào)用"); } /** * 后置通知: 方法執(zhí)行后調(diào)用,若方法出現(xiàn)異常,不執(zhí)行 */ @AfterReturning("logPointCut()") public void afterReturning() { System.out.println("后置通知: 方法執(zhí)行后調(diào)用,若方法出現(xiàn)異常,不執(zhí)行"); } /** * 最終通知:無論無何都會調(diào)用,類似于:try/catch中的finally */ @After("logPointCut()") public void after() { System.out.println("最終通知:無論無何都會調(diào)用,類似于:try/catch中的finally"); } /** * 異常通知:方法拋出異常時執(zhí)行 */ @AfterThrowing("logPointCut()") public void afterThrowing() { System.out.println("異常通知:方法拋出異常時執(zhí)行"); } /** * 環(huán)繞通知 * 既可以在目標方法之前織入增強動作,也可以在執(zhí)行目標方法之后織入增強動作; * 可以決定目標方法在什么時候執(zhí)行,如何執(zhí)行,甚至可以完全阻止目標目標方法的執(zhí)行; * 可以改變執(zhí)行目標方法的參數(shù)值,也可以改變執(zhí)行目標方法之后的返回值; 當需要改變目標方法的返回值時,只能使用Around方法; */ @Around("logPointCut()") public void around(ProceedingJoinPoint point) throws Throwable { // 獲取切點方法的名稱 String methodName = point.getSignature().getName(); // 獲取方法傳入?yún)?shù) Object[] params = point.getArgs(); //轉(zhuǎn)成字符串 List<Object> objects = Arrays.asList(params); objects.forEach(obj -> System.out.println(JSON.toJSONString(obj))); System.out.println("環(huán)繞通知"); } }
3、自定義切面注解類
package com.bz.aspect; import java.lang.annotation.*; /** * @author: BuZheng * @date: 2020-05-18 下午 2:02 */ @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface BzLogOperation { String value() default ""; }
4、接口測試
@ApiOperation("切面測試") @GetMapping("/aop") @BzLogOperation("切面測試") public ResultBean userList(@RequestParam(value = "keyWord") String keyWord) { log.info("### 切面測試 ###"); return new ResultBean(); }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot中通過8項配置優(yōu)化提升Tomcat性能的配置方法
優(yōu)化Spring Boot,Spring Cloud 應用程序中Tomcat的配置有助于提高性能和資源利用率,這篇文章主要介紹了SpringBoot中通過8項配置優(yōu)化提升Tomcat性能的配置方法,需要的朋友可以參考下2024-08-08關(guān)于Elasticsearch封裝公共索引增刪改查
索引是Elasticsearch中存儲數(shù)據(jù)的邏輯單元,類似于關(guān)系數(shù)據(jù)庫中的表,它包含多個文檔,每個文檔都是一個結(jié)構(gòu)化的JSON數(shù)據(jù)格式,在實際應用中,索引的使用與配置可以依據(jù)不同的方案進行,例如在Spring Boot項目中,可以選擇自動配置或者手動編寫配置類2024-10-10MyBatis完成CRUD?詳細細節(jié)內(nèi)容剖析
這篇文章主要介紹了MyBatis完成CRUD?詳細細節(jié)內(nèi)容剖析,本文通過圖文示例代碼給大家介紹的非常詳細,感興趣的朋友一起看看吧2024-05-05詳談cxf和axis兩種框架下的webservice客戶端開發(fā)
這篇文章主要介紹了詳談cxf和axis兩種框架下的webservice客戶端開發(fā),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08