Spring Boot如何通過(guò)自定義注解實(shí)現(xiàn)日志打印詳解
前言
在我們?nèi)粘5拈_(kāi)發(fā)過(guò)程中通過(guò)打印詳細(xì)的日志信息能夠幫助我們很好地去發(fā)現(xiàn)開(kāi)發(fā)過(guò)程中可能出現(xiàn)的Bug,特別是在開(kāi)發(fā)Controller層的接口時(shí),我們一般會(huì)打印出Request請(qǐng)求參數(shù)和Response響應(yīng)結(jié)果,但是如果這些打印日志的代碼相對(duì)而言還是比較重復(fù)的,那么我們可以通過(guò)什么樣的方式來(lái)簡(jiǎn)化日志打印的代碼呢?
SpringBoot 通過(guò)自定義注解實(shí)現(xiàn)權(quán)限檢查可參考我的博客:SpringBoot 通過(guò)自定義注解實(shí)現(xiàn)權(quán)限檢查
正文
Spring AOP
Spring AOP 即面向切面,是對(duì)OOP面向?qū)ο蟮囊环N延伸。
AOP機(jī)制可以讓開(kāi)發(fā)者把業(yè)務(wù)流程中的通用功能抽取出來(lái),單獨(dú)編寫(xiě)功能代碼。在業(yè)務(wù)流程執(zhí)行過(guò)程中,Spring框架會(huì)根據(jù)業(yè)務(wù)流程要求,自動(dòng)把獨(dú)立編寫(xiě)的功能代碼切入到流程的合適位置。
我們通過(guò)AOP機(jī)制可以實(shí)現(xiàn):Authentication 權(quán)限檢查、Caching 緩存、Context passing 內(nèi)容傳遞、Error handling 錯(cuò)誤處理、日志打印等功能,這里我們講一下怎么用Spring AOP來(lái)實(shí)現(xiàn)日志打印。
SpringBoot通過(guò)自定義注解實(shí)現(xiàn)日志打印
Maven依賴(lài)
<!--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: 用來(lái)修飾注解,是注解的注解,稱(chēng)為元注解。
- @Target:用來(lái)說(shuō)明對(duì)象的作用范圍
- @Documented:用來(lái)做標(biāo)記使用
/**
* 自定義注解用于打印Controller層方式日志
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface ControllerMethodLog {
}
這里特別講一下@Retention,按生命周期來(lái)劃分可分為3類(lèi):
- RetentionPolicy.SOURCE:注解只保留在源文件,當(dāng)Java文件編譯成class文件的時(shí)候,注解被遺棄(運(yùn)行時(shí)去動(dòng)態(tài)獲取注解信息);
- RetentionPolicy.CLASS:注解被保留到class文件,但jvm加載class文件時(shí)候被遺棄,這是默認(rèn)的生命周期(在編譯時(shí)進(jìn)行一些預(yù)處理操作);
- RetentionPolicy.RUNTIME:注解不僅被保存到class文件中,jvm加載class文件之后,仍然存在(做一些檢查性的操作);
這3個(gè)生命周期分別對(duì)應(yīng)于:Java源文件(.java文件) —> .class文件 —> 內(nèi)存中的字節(jié)碼。
Spring AOP切面方法的執(zhí)行順序
這里簡(jiǎn)單介紹一下,切面的執(zhí)行方法和其執(zhí)行順序:
- @Around 通知方法將目標(biāo)方法封裝起來(lái)
- @Before 通知方法會(huì)在目標(biāo)方法調(diào)用之前執(zhí)行
- @After 通知方法會(huì)在目標(biāo)方法返回或者異常后執(zhí)行
- @AfterReturning 通知方法會(huì)在目標(biāo)方法返回時(shí)執(zhí)行
- @Afterthrowing 通知方法會(huì)在目標(biāo)方法拋出異常時(shí)執(zhí)行
這里以一個(gè)返回正常的情況為例:(異常替換最后一步即可)

ControllerMethodLogAspect.class:用于打印日志的切面定義類(lèi)
注意要在啟動(dòng)類(lèi)掃描這個(gè)class,并且添加 @EnableAspectJAutoProxy(proxyTargetClass = true)
@Slf4j
@Component
@Aspect
public class ControllerMethodLogAspect {
@Pointcut("@annotation(com.xiyuan.demo.annotation.ControllerMethodLog)")
public void pointCut() {
}
/**
* 在切點(diǎn)運(yùn)行前執(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()));
}
/**
* 在切點(diǎn)運(yùn)行后,無(wú)異常時(shí)執(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));
}
}
驗(yàn)證
getUserById:根據(jù)id獲取用戶(hù)的信息
@GetMapping("/getUserById")
@ApiOperation(value = "根據(jù)用戶(hù)id獲取用戶(hù)")
@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控制臺(tái)打印信息如下:

總結(jié)
到此這篇關(guān)于Spring Boot如何通過(guò)自定義注解實(shí)現(xiàn)日志打印的文章就介紹到這了,更多相關(guān)SpringBoot自定義注解實(shí)現(xiàn)日志打印內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot實(shí)現(xiàn)阿里云快遞物流查詢(xún)的示例代碼
本文將基于springboot實(shí)現(xiàn)快遞物流查詢(xún),物流信息的獲取通過(guò)阿里云第三方實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2021-10-10
啟動(dòng)Tomcat報(bào)錯(cuò)Unsupported major.minor version xxx的解決方法
這篇文章主要為大家詳細(xì)介紹了啟動(dòng)Tomcat報(bào)錯(cuò)Unsupported major.minor version xxx的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
SpringBoot整合MyBatis實(shí)現(xiàn)CRUD操作項(xiàng)目實(shí)踐
本文主要介紹了SpringBoot整合MyBatis實(shí)現(xiàn)CRUD操作項(xiàng)目實(shí)踐,如何實(shí)現(xiàn)數(shù)據(jù)庫(kù)的CRUD創(chuàng)建、讀取、更新、刪除操作,具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02
Java IO流 File類(lèi)的常用API實(shí)例
這篇文章主要介紹了Java IO流 File類(lèi)的常用API實(shí)例的相關(guān)資料,需要的朋友參考下吧2017-05-05

