1. 程式人生 > >springboot aop 自定義註解方式實現一套完善的日誌記錄(完整原始碼)

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

一:功能簡介

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

主要記錄的資訊有: 操作人,方法名,引數,執行時間,操作型別(增刪改查),詳細描述,返回值。

二:專案結構圖

三: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>

四:程式碼實現

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";
    }
}

專案原始碼github地址: