SpringAOP实现记录操作日志

本文实例为大家分享了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;

推荐阅读