这篇文章主要介绍“怎么用Spring AOP实现记录操作日志”,在日常操作中,相信很多人在怎么用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;
到此,关于“怎么用Spring AOP实现记录操作日志”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。