AOP记录操作日志
创建数据库表
-- 操作日志
create table operate_log (
id int unsigned primary key auto_increment comment'id',
operate_user int unsigned comment '操作人员Id',
operate_time datetime comment '操作时间',
class_name varchar(100)comment '操作类',
method_name varchar(100)comment '操作的方法',
method_params varchar(1000)comment '方法参数',
return_value varchar(2000)comment '返回值',
cost_time bigint comment '方法执行耗时, 单位:ms'
) comment '操作日志表';
引入APO依赖
<!-- AOP依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
创建数据库对应的类
@Data
@NoArgsConstructor
@AllArgsConstructor
public class OperateLog {
private Integer id;
private Integer operateUser;// 操作人员的id
private LocalDateTime operateTime;//操作时间
private String className; // 操作类名
private String methodName ; //操作方法
private String methodParams; //方法参数
private String returnValue; //返回值
private Long costTime; //操作耗时
创建注解类
package com.it.anno;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME) //运行的时候
@Target(ElementType.METHOD) //目标是方法
public @interface Log {
}
创建AOP类
package com.it.aop;
import com.alibaba.fastjson.JSONObject;
import com.it.mapper.OperateLogMapper;
import com.it.pojo.OperateLog;
import com.it.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.Arrays;
@Slf4j
@Component
@Aspect //切面类
public class LogAspect {
@Autowired
private HttpServletRequest response;
@Autowired
private OperateLogMapper operateLogMapper;
@Around("@annotation(com.it.anno.Log)")
public Object recordLog(ProceedingJoinPoint joinPoint) throws Throwable{
//id 自增
//操作人的id
String jwt= response.getHeader("token");
Claims claims = JwtUtils.parseJWT(jwt);
Integer operateUser =(Integer) claims.get("id");
//操作时间
LocalDateTime operateTime = LocalDateTime.now();
//操作类名
String className = joinPoint.proceed().getClass().getName();
//操作方法名
String methodName = joinPoint.getSignature().getName();
//操作方法参数
Object[] args = joinPoint.getArgs();
String methodParams = Arrays.toString(args);
//记录时间 (开始)
long begin = System.currentTimeMillis();
//调用原始的目标方法运行
Object proceed = joinPoint.proceed();
//记录时间 (结束)
long end = System.currentTimeMillis();
//方法返回值
String returnValue = JSONObject.toJSONString(proceed);
//耗时时间
long costTime = end - begin;
//记录日志
OperateLog operateLog=new OperateLog(null,operateUser,operateTime,className,methodName,methodParams,returnValue,costTime);
operateLogMapper.insert(operateLog);
log.info("AOP记录操作日志:{}",operateLog);
return proceed;
}
}
每个方法上把自定义的@Log注解引入进来
/*根据ID删除部门*/
//@DeleteMapping("/{id}")
@Log
@DeleteMapping("/depts/{id}")
public Result delete( @PathVariable Integer id) throws Exception {
log.info("根据ID删除部门:{}",id);
deptservec.delete(id);
return Result.success();
}
//新增部门
// @PostMapping
@Log
@PostMapping ("/depts")
public Result add( @PathVariable Dept dept) {
log .info("新增部门:{}",dept);
deptservec.add(dept);
return Result.success();
}