Spring Boot如何通過自定義注解實現(xiàn)日志打印詳解
前言
在我們?nèi)粘5拈_發(fā)過程中通過打印詳細(xì)的日志信息能夠幫助我們很好地去發(fā)現(xiàn)開發(fā)過程中可能出現(xiàn)的Bug,特別是在開發(fā)Controller層的接口時,我們一般會打印出Request請求參數(shù)和Response響應(yīng)結(jié)果,但是如果這些打印日志的代碼相對而言還是比較重復(fù)的,那么我們可以通過什么樣的方式來簡化日志打印的代碼呢?
SpringBoot 通過自定義注解實現(xiàn)權(quán)限檢查可參考我的博客:SpringBoot 通過自定義注解實現(xiàn)權(quán)限檢查
正文
Spring AOP
Spring AOP 即面向切面,是對OOP面向?qū)ο蟮囊环N延伸。
AOP機(jī)制可以讓開發(fā)者把業(yè)務(wù)流程中的通用功能抽取出來,單獨編寫功能代碼。在業(yè)務(wù)流程執(zhí)行過程中,Spring框架會根據(jù)業(yè)務(wù)流程要求,自動把獨立編寫的功能代碼切入到流程的合適位置。
我們通過AOP機(jī)制可以實現(xiàn):Authentication 權(quán)限檢查、Caching 緩存、Context passing 內(nèi)容傳遞、Error handling 錯誤處理、日志打印等功能,這里我們講一下怎么用Spring AOP來實現(xiàn)日志打印。
SpringBoot通過自定義注解實現(xiàn)日志打印
Maven依賴
<!--lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.2</version> <optional>true</optional> </dependency> <!--Spring AOP--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
ControllerMethodLog.class自定義注解
- @Retention: 用來修飾注解,是注解的注解,稱為元注解。
- @Target:用來說明對象的作用范圍
- @Documented:用來做標(biāo)記使用
/** * 自定義注解用于打印Controller層方式日志 */ @Documented @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD}) public @interface ControllerMethodLog { }
這里特別講一下@Retention,按生命周期來劃分可分為3類:
- RetentionPolicy.SOURCE:注解只保留在源文件,當(dāng)Java文件編譯成class文件的時候,注解被遺棄(運行時去動態(tài)獲取注解信息);
- RetentionPolicy.CLASS:注解被保留到class文件,但jvm加載class文件時候被遺棄,這是默認(rèn)的生命周期(在編譯時進(jìn)行一些預(yù)處理操作);
- RetentionPolicy.RUNTIME:注解不僅被保存到class文件中,jvm加載class文件之后,仍然存在(做一些檢查性的操作);
這3個生命周期分別對應(yīng)于:Java源文件(.java文件) —> .class文件 —> 內(nèi)存中的字節(jié)碼。
Spring AOP切面方法的執(zhí)行順序
這里簡單介紹一下,切面的執(zhí)行方法和其執(zhí)行順序:
- @Around 通知方法將目標(biāo)方法封裝起來
- @Before 通知方法會在目標(biāo)方法調(diào)用之前執(zhí)行
- @After 通知方法會在目標(biāo)方法返回或者異常后執(zhí)行
- @AfterReturning 通知方法會在目標(biāo)方法返回時執(zhí)行
- @Afterthrowing 通知方法會在目標(biāo)方法拋出異常時執(zhí)行
這里以一個返回正常的情況為例:(異常替換最后一步即可)
ControllerMethodLogAspect.class:用于打印日志的切面定義類
注意要在啟動類掃描這個class,并且添加 @EnableAspectJAutoProxy(proxyTargetClass = true)
@Slf4j @Component @Aspect public class ControllerMethodLogAspect { @Pointcut("@annotation(com.xiyuan.demo.annotation.ControllerMethodLog)") public void pointCut() { } /** * 在切點運行前執(zhí)行該方法 */ @Before("pointCut()") public void doBefore(JoinPoint joinPoint) { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); ControllerMethodLog annotation = method.getAnnotation(ControllerMethodLog.class); if (Objects.isNull(annotation)) { return; } String methodName = method.getDeclaringClass().getSimpleName() + "." + method.getName(); log.info("start {}:入?yún)ⅲ簕}", methodName, JSON.toJSONString(joinPoint.getArgs())); } /** * 在切點運行后,無異常時執(zhí)行該方法 * * @param joinPoint * @param result */ @AfterReturning(value = "pointCut()", returning = "result") public void afterReturn(JoinPoint joinPoint, Object result) { MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method = signature.getMethod(); ControllerMethodLog annotation = method.getAnnotation(ControllerMethodLog.class); if (Objects.isNull(annotation)) { return; } String methodName = method.getDeclaringClass().getSimpleName() + "." + method.getName(); log.info("end {}:響應(yīng):{}", methodName, JSON.toJSONString(result)); } }
驗證
getUserById:根據(jù)id獲取用戶的信息
@GetMapping("/getUserById") @ApiOperation(value = "根據(jù)用戶id獲取用戶") @ControllerMethodLog public ResponseResult getUserById(@RequestParam(name = "id", required = true) String id) { UserInfoPojo userInfoPojo = userService.getUserById(id); return ResponseResult.success(userInfoPojo, ConstantsUtil.QUERY_SUCCESS); }
Swagger接口信息如下:
IDEA控制臺打印信息如下:
總結(jié)
到此這篇關(guān)于Spring Boot如何通過自定義注解實現(xiàn)日志打印的文章就介紹到這了,更多相關(guān)SpringBoot自定義注解實現(xiàn)日志打印內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot實現(xiàn)阿里云快遞物流查詢的示例代碼
本文將基于springboot實現(xiàn)快遞物流查詢,物流信息的獲取通過阿里云第三方實現(xiàn),具有一定的參考價值,感興趣的可以了解一下2021-10-10啟動Tomcat報錯Unsupported major.minor version xxx的解決方法
這篇文章主要為大家詳細(xì)介紹了啟動Tomcat報錯Unsupported major.minor version xxx的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11SpringBoot整合MyBatis實現(xiàn)CRUD操作項目實踐
本文主要介紹了SpringBoot整合MyBatis實現(xiàn)CRUD操作項目實踐,如何實現(xiàn)數(shù)據(jù)庫的CRUD創(chuàng)建、讀取、更新、刪除操作,具有一定的參考價值,感興趣的可以了解一下2024-02-02