Spring AOP實現記錄操作日志

本文實例為大傢分享瞭Spring AOP實現記錄操作日志的具體代碼,供大傢參考,具體內容如下

1 添加maven依賴

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

2 自定義操作日志註解

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface OperationLog {
    //操作類型
    int type() default GlobalConstant.OPERATE_TYPE_QUERY;

    //操作模塊
    String module() default "";
}

3 定義切面類

import com.admin.annotation.OperationLog;
import com.admin.sys.dao.SysOperateLogDao;
import com.admin.sys.dao.SysUserDao;
import com.admin.sys.entity.SysOperateLog;
import com.admin.util.JwtUtil;
import org.aspectj.lang.ProceedingJoinPoint;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;


@Aspect
@Component
public class OperationLogAspect {
    @Resource
    private HttpServletRequest request;

    @Resource
    private SysUserDao sysUserDao;

    @Resource
    private SysOperateLogDao sysOperateLogDao;

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Pointcut("@annotation(com.admin.annotation.OperationLog)")
    public void pointCut() {
    }

    @Around("pointCut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        logger.info("方法執行前");
        //從切面織入點處通過反射機制獲取織入點處的方法
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        logger.info(method.toString());
        //執行方法
        Object obj = joinPoint.proceed();
        OperationLog operationLog = method.getAnnotation(OperationLog.class);
        //通過token獲取用戶ID
        Long userId = JwtUtil.getUserIdByToken(request);
        String userName = this.sysUserDao.selectById(userId).getUserName();
        String operateModule = operationLog.module();
        int operateType = operationLog.type();
        SysOperateLog sysOperateLog = SysOperateLog.builder()
                .userName(userName)
                .operateModule(operateModule)
                .operateType(operateType)
                .build();
        //記錄操作日志
        this.sysOperateLogDao.insert(sysOperateLog);
        logger.info("方法執行後");
        return obj;
    }
}

4 測試

通過postman發送請求,如下圖表示記錄操作日志成功

操作日志表的sql語句如下

CREATE TABLE `sys_operate_log`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `user_name` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL COMMENT '操作者用戶名',
  `operate_module` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL COMMENT '操作模塊',
  `operate_type` int(1) NULL DEFAULT NULL COMMENT '操作類型(1:查詢  2:插入  3:更改  4:刪除)',
  `operate_time` datetime NULL DEFAULT NULL COMMENT '操作時間',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin COMMENT = '操作日志表' ROW_FORMAT = Dynamic;

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。

推薦閱讀: