SpringAOP實(shí)現(xiàn)日志收集管理功能(步驟詳解)
第一步引入必要的依賴
<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)建自定義日志注解類(lèi)
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è)計(jì),掃描上面的注解
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ù)庫(kù)的日志表里面,等到有客戶反饋的時(shí)候好去查詢?nèi)罩?
System.out.println(
"methodName: " + methodName + "; className: " + className + "; paramsMap:" + paramsMap);
// 也可以記錄請(qǐng)求當(dāng)前接口耗費(fèi)的時(shí)間
} catch (Exception e) {
// 如果在日志記錄的過(guò)程中出現(xiàn)問(wèn)題,那么要處理一下異常,不要直接拋出,此處做成消息隊(duì)列通知或者打印日志
}
// 此處要進(jìn)行返回代理的對(duì)象,如果沒(méi)有返回的話,前端是收不到數(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";
}
}
第五步測(cè)試


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

