SpringAOP實現(xiàn)日志收集管理功能(步驟詳解)
更新時間:2022年03月31日 15:20:59 作者:張泓銳
這篇文章主要介紹了SpringAOP實現(xiàn)日志收集管理功能,本文分步驟通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
第一步引入必要的依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.3.15</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.2</version>
</dependency>第二步創(chuàng)建自定義日志注解類
package com.example.aop.log.annotion;
import java.lang.annotation.*;
/**
* @author zhr_java@163.com
* @date 2022/3/29 20:56
*/
@Inherited
@Target({ElementType.METHOD})
@Retention(value = RetentionPolicy.RUNTIME)
@Documented
public @interface LogNotice {}第三步切面設(shè)計,掃描上面的注解
package com.example.aop.log.aop;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.DefaultParameterNameDiscoverer;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
/**
* @author zhr_java@163.com
* @date 2022/3/29 20:59
*/
@Aspect
@Component
public class LogAspect {
@Pointcut("@annotation(com.example.aop.log.annotion.LogNotice)")
public void doLog() {}
@Around("doLog()")
public Object insertLog(ProceedingJoinPoint joinPoint) throws Throwable {
Object object = joinPoint.proceed();
try {
HashMap paramsMap;
Signature signature = joinPoint.getSignature();
Method method = ((MethodSignature) signature).getMethod();
String methodName = method.getName();
String className = method.getDeclaringClass().getName();
paramsMap = (HashMap) getParams(joinPoint, method);
// 打印日志,可以記錄到數(shù)據(jù)庫的日志表里面,等到有客戶反饋的時候好去查詢?nèi)罩?
System.out.println(
"methodName: " + methodName + "; className: " + className + "; paramsMap:" + paramsMap);
// 也可以記錄請求當(dāng)前接口耗費的時間
} catch (Exception e) {
// 如果在日志記錄的過程中出現(xiàn)問題,那么要處理一下異常,不要直接拋出,此處做成消息隊列通知或者打印日志
}
// 此處要進行返回代理的對象,如果沒有返回的話,前端是收不到數(shù)據(jù)的
return object;
}
private Map<String, Object> getParams(ProceedingJoinPoint joinPoint, Method method) {
// get parameter names
String[] parameterNames = new DefaultParameterNameDiscoverer().getParameterNames(method);
Object[] args = joinPoint.getArgs();
Map<String, Object> params = new HashMap<>(8);
if (parameterNames != null && parameterNames.length != 0) {
for (int i = 0; i < parameterNames.length; i++) {
params.put(parameterNames[i], args[i]);
}
return params;
}
第四步把自定義的注解放到你的接口上面
package com.example.demo.controller;
import com.example.aop.log.annotion.LogNotice;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author zhr_java@163.com
* @date 2022/3/29 20:50
*/
@RestController
@RequestMapping("/person")
public class PersonController {
//在此處加上你自定義的注解(@LogNotice)
@LogNotice
@GetMapping("get_person")
public String getPerson(Integer numbers) {
return "123";
}
}
第五步測試


到此這篇關(guān)于SpringAOP實現(xiàn)日志收集管理功能的文章就介紹到這了,更多相關(guān)SpringAOP日志收集管理內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JPA @Query時,無法使用limit函數(shù)的問題及解決
這篇文章主要介紹了JPA @Query時,無法使用limit函數(shù)的問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
Spring boot詳解緩存redis實現(xiàn)定時過期方法
本篇文章分享的就是spring boot中的一個輪子,spring cache注解的方式實現(xiàn)接口數(shù)據(jù)緩存。默認的配置想非常簡單,但是有一個弊端是緩存數(shù)據(jù)為永久緩存,本次將介紹如何設(shè)置接口緩存數(shù)據(jù)的過期時間2022-07-07
Eclipse轉(zhuǎn)Itellij IDEA導(dǎo)入Git/svn本地項目的詳細步驟
這篇文章主要介紹了Eclipse轉(zhuǎn)Itellij IDEA導(dǎo)入Git/svn本地項目,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10
mybatis-plus開啟sql打印的三種方式總結(jié)
這篇文章主要給大家介紹了mybatisplus開啟sql打印的三種方式,文章通過代碼示例介紹的非常詳細,對大家的學(xué)習(xí)或工作有一定的參考價值,需要的朋友可以參考下2023-11-11

