springboot aop 自定義註解方式實現一套完善的日誌記錄(完整源碼)

来源:https://www.cnblogs.com/wenjunwei/archive/2018/09/13/9639909.html
-Advertisement-
Play Games

本文主要記錄如何使用aop切麵的方式來實現日誌記錄功能。 主要記錄的信息有: 操作人,方法名,參數,運行時間,操作類型(增刪改查),詳細描述,返回值。 ...


版權聲明:本文為博主原創文章,歡迎轉載,轉載請註明作者、原文超鏈接

一:功能簡介

本文主要記錄如何使用aop切麵的方式來實現日誌記錄功能。

主要記錄的信息有: 操作人,方法名,參數,運行時間,操作類型(增刪改查),詳細描述,返回值。

二:項目結構圖

三:代碼實現

1.配置文件

這裡只有兩個配置:
1)server.port=11000,設置項目啟動的埠號,防止被其他服務占用;
2)spring.aop.auto=true,開啟spring的aop配置,簡單明瞭,不需要多配置其他的配置或註解。
application.yml文件
server:
  port: 11000
spring:
  aop:
    auto: true #啟動aop配置

 2.AOP切點類

這個是最主要的類,可以使用自定義註解或針對包名實現AOP增強。

1)這裡實現了對自定義註解的環繞增強切點,對使用了自定義註解的方法進行AOP切麵處理;

2)對方法運行時間進行監控;

3)對方法名,參數名,參數值,對日誌描述的優化處理;

在方法上增加@Aspect 註解聲明切麵,使用@Pointcut 註解定義切點,標記方法。

 使用切點增強的時機註解:@Before,@Around,@AfterReturning,@AfterThrowing,@After

package com.wwj.springboot.aop;

import com.alibaba.fastjson.JSON;
import com.wwj.springboot.annotation.OperationLogDetail;
import com.wwj.springboot.model.OperationLog;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * Created by IntelliJ IDEA
 *
 * @author weiwenjun
 * @date 2018/9/12
 */
@Aspect
@Component
public class LogAspect {

    /**
     * 此處的切點是註解的方式,也可以用包名的方式達到相同的效果
     * '@Pointcut("execution(* com.wwj.springboot.service.impl.*.*(..))")'
     */
    @Pointcut("@annotation(com.wwj.springboot.annotation.OperationLogDetail)")
    public void operationLog(){}


    /**
     * 環繞增強,相當於MethodInterceptor
     */
    @Around("operationLog()")
    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
        Object res = null;
        long time = System.currentTimeMillis();
        try {
            res =  joinPoint.proceed();
            time = System.currentTimeMillis() - time;
            return res;
        } finally {
            try {
                //方法執行完成後增加日誌
                addOperationLog(joinPoint,res,time);
            }catch (Exception e){
                System.out.println("LogAspect 操作失敗:" + e.getMessage());
                e.printStackTrace();
            }
        }
    }

    private void addOperationLog(JoinPoint joinPoint, Object res, long time){
        MethodSignature signature = (MethodSignature)joinPoint.getSignature();
        OperationLog operationLog = new OperationLog();
        operationLog.setRunTime(time);
        operationLog.setReturnValue(JSON.toJSONString(res));
        operationLog.setId(UUID.randomUUID().toString());
        operationLog.setArgs(JSON.toJSONString(joinPoint.getArgs()));
        operationLog.setCreateTime(new Date());
        operationLog.setMethod(signature.getDeclaringTypeName() + "." + signature.getName());
        operationLog.setUserId("#{currentUserId}");
        operationLog.setUserName("#{currentUserName}");
        OperationLogDetail annotation = signature.getMethod().getAnnotation(OperationLogDetail.class);
        if(annotation != null){
            operationLog.setLevel(annotation.level());
            operationLog.setDescribe(getDetail(((MethodSignature)joinPoint.getSignature()).getParameterNames(),joinPoint.getArgs(),annotation));
            operationLog.setOperationType(annotation.operationType().getValue());
            operationLog.setOperationUnit(annotation.operationUnit().getValue());
        }
        //TODO 這裡保存日誌
        System.out.println("記錄日誌:" + operationLog.toString());
//        operationLogService.insert(operationLog);
    }

    /**
     * 對當前登錄用戶和占位符處理
     * @param argNames 方法參數名稱數組
     * @param args 方法參數數組
     * @param annotation 註解信息
     * @return 返回處理後的描述
     */
    private String getDetail(String[] argNames, Object[] args, OperationLogDetail annotation){

        Map<Object, Object> map = new HashMap<>(4);
        for(int i = 0;i < argNames.length;i++){
            map.put(argNames[i],args[i]);
        }

        String detail = annotation.detail();
        try {
            detail = "'" + "#{currentUserName}" + "'=》" + annotation.detail();
            for (Map.Entry<Object, Object> entry : map.entrySet()) {
                Object k = entry.getKey();
                Object v = entry.getValue();
                detail = detail.replace("{{" + k + "}}", JSON.toJSONString(v));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return detail;
    }

    @Before("operationLog()")
    public void doBeforeAdvice(JoinPoint joinPoint){
        System.out.println("進入方法前執行.....");

    }

    /**
     * 處理完請求,返回內容
     * @param ret
     */
    @AfterReturning(returning = "ret", pointcut = "operationLog()")
    public void doAfterReturning(Object ret) {
        System.out.println("方法的返回值 : " + ret);
    }

    /**
     * 後置異常通知
     */
    @AfterThrowing("operationLog()")
    public void throwss(JoinPoint jp){
        System.out.println("方法異常時執行.....");
    }


    /**
     * 後置最終通知,final增強,不管是拋出異常或者正常退出都會執行
     */
    @After("operationLog()")
    public void after(JoinPoint jp){
        System.out.println("方法最後執行.....");
    }

}

3.自定義註解

package com.wwj.springboot.annotation;

import com.wwj.springboot.enums.OperationType;
import com.wwj.springboot.enums.OperationUnit;

import java.lang.annotation.*;

/**
 * Created by IntelliJ IDEA
 *
 * @author weiwenjun
 * @date 2018/9/12
 */
//@OperationLogDetail(detail = "通過手機號[{{tel}}]獲取用戶名",level = 3,operationUnit = OperationUnit.USER,operationType = OperationType.SELECT)
@Documented
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface OperationLogDetail {

    /**
     * 方法描述,可使用占位符獲取參數:{{tel}}
     */
    String detail() default "";

    /**
     * 日誌等級:自己定,此處分為1-9
     */
    int level() default 0;

    /**
     * 操作類型(enum):主要是select,insert,update,delete
     */
    OperationType operationType() default OperationType.UNKNOWN;

    /**
     * 被操作的對象(此處使用enum):可以是任何對象,如表名(user),或者是工具(redis)
     */
    OperationUnit operationUnit() default OperationUnit.UNKNOWN;
}

4.註解用到的枚舉類型

package com.wwj.springboot.enums;

/**
 * Created by IntelliJ IDEA
 *
 * @author weiwenjun
 * @date 2018/9/12
 */
public enum OperationType {
    /**
     * 操作類型
     */
    UNKNOWN("unknown"),
    DELETE("delete"),
    SELECT("select"),
    UPDATE("update"),
    INSERT("insert");

    private String value;

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    OperationType(String s) {
        this.value = s;
    }
}
package com.wwj.springboot.enums;

/**
 * Created by IntelliJ IDEA
 * 被操作的單元
 * @author weiwenjun
 * @date 2018/9/12
 */
public enum OperationUnit {
    /**
     * 被操作的單元
     */
    UNKNOWN("unknown"),
    USER("user"),
    EMPLOYEE("employee"),
    Redis("redis");

    private String value;

    OperationUnit(String value) {
        this.value = value;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

5.日誌記錄對象

package com.wwj.springboot.model;

import java.util.Date;

/**
 * Created by IntelliJ IDEA
 *
 * @author weiwenjun
 * @date 2018/9/12
 */
public class OperationLog {

    private String id;
    private Date createTime;
    /**
     * 日誌等級
     */
    private Integer level;
    /**
     * 被操作的對象
     */
    private String operationUnit;
    /**
     * 方法名
     */
    private String method;
    /**
     * 參數
     */
    private String args;
    /**
     * 操作人id
     */
    private String userId;
    /**
     * 操作人
     */
    private String userName;
    /**
     * 日誌描述
     */
    private String describe;
    /**
     * 操作類型
     */
    private String operationType;
    /**
     * 方法運行時間
     */
    private Long runTime;
    /**
     * 方法返回值
     */
    private String returnValue;

    @Override
    public String toString() {
        return "OperationLog{" +
                "id='" + id + '\'' +
                ", createTime=" + createTime +
                ", level=" + level +
                ", operationUnit='" + operationUnit + '\'' +
                ", method='" + method + '\'' +
                ", args='" + args + '\'' +
                ", userId='" + userId + '\'' +
                ", userName='" + userName + '\'' +
                ", describe='" + describe + '\'' +
                ", operationType='" + operationType + '\'' +
                ", runTime=" + runTime +
                ", returnValue='" + returnValue + '\'' +
                '}';
    }

    public Long getRunTime() {
        return runTime;
    }

    public void setRunTime(Long runTime) {
        this.runTime = runTime;
    }

    public String getReturnValue() {
        return returnValue;
    }

    public void setReturnValue(String returnValue) {
        this.returnValue = returnValue;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Integer getLevel() {
        return level;
    }

    public void setLevel(Integer level) {
        this.level = level;
    }

    public String getOperationUnit() {
        return operationUnit;
    }

    public void setOperationUnit(String operationUnit) {
        this.operationUnit = operationUnit;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getArgs() {
        return args;
    }

    public void setArgs(String args) {
        this.args = args;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getDescribe() {
        return describe;
    }

    public void setDescribe(String describe) {
        this.describe = describe;
    }

    public String getOperationType() {
        return operationType;
    }

    public void setOperationType(String operationType) {
        this.operationType = operationType;
    }
}

6.springboot啟動類

package com.wwj.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Created by IntelliJ IDEA
 *
 * @author weiwenjun
 * @date 2018/9/12
 */
@SpringBootApplication
public class AopApplication {

    public static void main(String[] args) {
        SpringApplication.run(AopApplication.class);
    }
}

7.controller類

package com.wwj.springboot.controller;

import com.wwj.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by IntelliJ IDEA
 *
 * @author weiwenjun
 * @date 2018/9/12
 */
@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 訪問路徑 http://localhost:11000/user/findUserNameByTel?tel=1234567
     * @param tel 手機號
     * @return userName
     */
    @ResponseBody
    @RequestMapping("/findUserNameByTel")
    public String findUserNameByTel(@RequestParam("tel") String tel){
        return userService.findUserName(tel);
    }
}

8.Service類

package com.wwj.springboot.service;

/**
 * Created by IntelliJ IDEA
 *
 * @author weiwenjun
 * @date 2018/9/13
 */
public interface UserService {

    /**
     * 獲取用戶信息
     * @return
     * @param tel
     */
    String findUserName(String tel);
}

9.ServiceImpl類

package com.wwj.springboot.service.impl;

import com.wwj.springboot.annotation.OperationLogDetail;
import com.wwj.springboot.enums.OperationType;
import com.wwj.springboot.enums.OperationUnit;
import com.wwj.springboot.service.UserService;
import org.springframework.stereotype.Service;

/**
 * Created by IntelliJ IDEA
 *
 * @author weiwenjun
 * @date 2018/9/13
 */
@Service
public class UserServiceImpl implements UserService {

    @OperationLogDetail(detail = "通過手機號[{{tel}}]獲取用戶名",level = 3,operationUnit = OperationUnit.USER,operationType = OperationType.SELECT)
    @Override
    public String findUserName(String tel) {
        System.out.println("tel:" + tel);
        return "zhangsan";
    }
}

四:MAVEM依賴

本項目有兩個pom文件,父類的pom文件主要作用是對子類pom文件依賴的版本號進行統一管理。

1.最外層的pom文件配置如下

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.wwj.springboot</groupId>
    <artifactId>springboot</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>springboot-aop</module>
    </modules>

    <!-- Inherit defaults from Spring Boot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
    </parent>
    <properties>
        <fastjson.version>1.2.49</fastjson.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <!-- Import dependency management from Spring Boot -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.0.4.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>${fastjson.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

2.子pom文件配置如下

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springboot</artifactId>
        <groupId>com.wwj.springboot</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springboot-aop</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- spring-boot aop依賴配置引入 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
        </dependency>
    </dependencies>

</project>

五:運行結果

進入方法前執行.....
tel:1234567
記錄日誌:OperationLog{id='cd4b5ba7-7580-4989-a75e-51703f0dfbfc', createTime=Fri Sep 14 08:54:55 CST 2018, level=3, operationUnit='user', method='com.wwj.springboot.service.impl.UserServiceImpl.findUserName', args='["1234567"]', userId='#{currentUserId}', userName='#{currentUserName}', describe=''#{currentUserName}'=》通過手機號["1234567"]獲取用戶名', operationType='select', runTime=4, returnValue='"zhangsan"'}
方法最後執行.....
方法的返回值 : zhangsan

 

項目源碼github地址:

https://github.com/weiwenjungithub/springboot.git

 

感謝您的閱讀,如果您覺得閱讀本文對您有幫助,請點一下“推薦”按鈕。本文歡迎各位轉載,但是轉載文章之後必須在文章頁面中給出作者和原文連接

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 單例概述 單例意即類在整個工程里只能有一個實例。單例通常應用在如下場景中,類的構造是一個非常耗時的過程,並且,它沒有多次構造的必要性。例如。你可以打開一個資料庫連接,只在此連接上進行資料庫操作。 那麼怎確保它在整個工程中只有一個實例呢?我們可以通過將構造函數的訪問許可權設置為private,並輔助其它 ...
  • java基礎: 1、==和equals的區別: 基本類型和引用類型 ==用來比較值,equals是比較對象的引用,即是否指向同一個對象? String str1 = new String("hello");String str2 = new String("hello"); System.out.p ...
  • 題目:括弧匹配 題目來源:https://blog.csdn.net/lizi_stdio/article/details/76618908 題目介紹:輸入一個字元串,裡面可能包含“()”、“ [ ] ”、" { } "三種括弧,要求程式判斷這個字元串里的括弧是否成對出現且嵌套關係正確,若成對出現且 ...
  • c/c++ 標準容器 forward_list, resize, 重新定位迭代器 1,forward_list特有的方法: + insert_after + emplace_after + erase_after 2,容器的插入刪除操作後的註意事項 + 必須保證每次改變容器的操作後都正確地重新定位迭 ...
  • 正則表達式是對字元串操作的一種邏輯公式,就是用事先定義好的一些特定字元、及這些特定字元的組合,組成一個“規則字元串”,這個“規則字元串”用來表達對字元串的一種過濾邏輯。 在python中正則表達式被封裝到了re模塊,通過引入re模塊來使用正則表達式 re模塊中有很多正則表達式處理函數,首先用find ...
  • c/c++ 標準順序容器 容器的訪問,刪除 操作 pop_front:vector,string不支持 pop_back:forward_list不支持 知識點 1,front, back, at 成員函數的使用,對應代碼里的test1 2,刪除最後一個元素pop_back, 刪除第一個元素pop_ ...
  • 明天老王要給我們講JVM的知識,提前發了一個小Demo給我們看,代碼如下: 運行上述代碼,結果毫無疑問,電腦瞬間開始狂躁起來,過了十幾秒,然後G了 基於JDK1.8運行的,估計老版本會崩的更快。。。 如果不計算記憶體,這個HashMap一共要插入4000*4000*4個對象,但是其實只有4個是不重覆的 ...
  • 據說Visual Studio Code(VS Code)的諸多好處,瞭解了一下果然很喜歡,我喜歡它的原因主要有3個,一是VS Code開源且跨平臺,二是因為其界面非常酷,三是可以我的大所屬代碼需求,除此之外當然還有強大的好奇心。使用VScode編寫第一個Python程式“one.py”,並將其打包... ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...