前言:
任何一个项目都会有一个用户操作日志(也叫行为日志)的模块,它主要用来记录某个用户做了某个操作,当出现操作失败时,通过日志就可以快速的查找是哪个用户在哪个模块出现了错误,以便于开发人员快速定位问题所在。
实现这一功能一般有两种方法:第一种就是很传统的做法,就是在每个模块进行插入日志的操作(不推荐),这种做法虽然实现了记录用户的操作,但很繁琐而且基本上是重复的工作。
第二种就是使用Spring的AOP来实现记录用户操作,也是推荐的现如今都使用的一种做法。它的优势在于这种记录用户操作的代码独立于其他业务逻辑代码,不仅实现了解耦,而且避免了冗余代码。
具体实现步骤在pom.xml中添加AOP依赖
org.springframework.boot
spring-boot-starter-aop
`设计操作日志记录表
新增日志实体类、dao层 接口
自定义操作日志记录的注解
package com.example.springcloud.aop;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @author lyz
* @title: OperationLog
* @projectName springcloud
* @date 2020/9/23
* @description: 自定义操作日志注解
*/
@Target(ElementType.METHOD)//注解放置的目标位置即方法级别
@Retention(RetentionPolicy.RUNTIME)//注解在哪个阶段执行
@Documented
public @interface OperationLogAnnotation {
String operModul() default ""; // 操作模块
String operType() default ""; // 操作类型
String operDesc() default ""; // 操作说明
}自定义操作日志切面类,该类是将操作日志保存到数据库
package com.example.springcloud.aop;
import com.example.springcloud.dao.OperationLogDao;</