这篇文章主要为大家详细介绍了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;
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程学习网。
沃梦达教程
本文标题为:Spring AOP实现记录操作日志
基础教程推荐
猜你喜欢
- Java并发编程进阶之线程控制篇 2023-03-07
- Java数据结构之对象比较详解 2023-03-07
- java基础知识之FileInputStream流的使用 2023-08-11
- JDK数组阻塞队列源码深入分析总结 2023-04-18
- Java实现查找文件和替换文件内容 2023-04-06
- Java文件管理操作的知识点整理 2023-05-19
- springboot自定义starter方法及注解实例 2023-03-31
- ConditionalOnProperty配置swagger不生效问题及解决 2023-01-02
- java实现多人聊天系统 2023-05-19
- Java实现线程插队的示例代码 2022-09-03