Spring AOP實現(xiàn)記錄操作日志
本文實例為大家分享了Spring AOP實現(xiàn)記錄操作日志的具體代碼,供大家參考,具體內(nèi)容如下
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("方法執(zhí)行前");
? ? ? ? //從切面織入點處通過反射機制獲取織入點處的方法
? ? ? ? MethodSignature signature = (MethodSignature) joinPoint.getSignature();
? ? ? ? Method method = signature.getMethod();
? ? ? ? logger.info(method.toString());
? ? ? ? //執(zhí)行方法
? ? ? ? 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("方法執(zhí)行后");
? ? ? ? return obj;
? ? }
}4 測試

通過postman發(fā)送請求,如下圖表示記錄操作日志成功

操作日志表的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;
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java與Scala創(chuàng)建List與Map的實現(xiàn)方式
這篇文章主要介紹了Java與Scala創(chuàng)建List與Map的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10
SpringBoot實現(xiàn)動態(tài)數(shù)據(jù)源切換的方法總結(jié)
項目開發(fā)中經(jīng)常會遇到多數(shù)據(jù)源同時使用的場景,比如冷熱數(shù)據(jù)的查詢等情況,所以接下來本文就來介紹一下如何使用實現(xiàn)自定義注解的形式來實現(xiàn)動態(tài)數(shù)據(jù)源切換吧2023-12-12
Java8 使用工廠方法supplyAsync創(chuàng)建CompletableFuture實例
這篇文章主要介紹了Java8 使用工廠方法supplyAsync創(chuàng)建CompletableFuture實例,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11
深入Spring Boot實現(xiàn)對Fat Jar jsp的支持
這篇文章主要介紹了深入Spring Boot實現(xiàn)對Fat Jar jsp的支持,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06

