基于SpringBoot的操作日志管理(AOP+自定义注解方式)

1.数据库设计

2.自定义注解编写

import com.cdls.carp.business.enumerate.MethodTypeEnum;

import java.lang.annotation.*;

/**
 * @Description: 日志操作注解
 * @Author yjw
 * @Date 2020/4/3
 **/
@Documented
@Target(value = ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Log {

    //  执行模块
    String module() default "";

    //  操作内容
    String content() default "";

    //  请求路径
    String actionUrl() default "";

    //  操作类型:1新增,2修改,3删除, 默认为1
    MethodTypeEnum type() default MethodTypeEnum.INSERT;
}

3.Spring AOP编写

import com.cdls.carp.business.annotation.Log;
import com.cdls.carp.business.entity.LogEntity;
import com.cdls.carp.business.service.LogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Date;

/**
 * @Description: 日志操作切面
 * @Author yjw
 * @Date 2020/4/3
 **/
@Aspect
@Component
public class LogAspect {

    @Autowired
    private LogService logService;

    /**
     * 日志切入点
     */
    @Pointcut("@annotation(com.cdls.carp.business.annotation.Log)")
    public void logPointCut(){}

    @AfterReturning(pointcut = "logPointCut()")
    public void doAfter(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        Method method = currentMethod(joinPoint,methodName);
        Log log = method.getAnnotation(Log.class);

        LogEntity logEntity = this.initLog(log, methodName);
        logEntity.setCommit(true);
        logService.save(logEntity);
    }

    @AfterThrowing(pointcut = "logPointCut()", throwing = "e")
    public void doAfterThrowing(JoinPoint joinPoint, Throwable e) {
        String methodName = joinPoint.getSignature().getName();
        Method method = currentMethod(joinPoint,methodName);
        Log log = method.getAnnotation(Log.class);

        LogEntity logEntity = this.initLog(log, methodName);
        logEntity.setCommit(false);
        logEntity.setErrorMessage(e + "");
        logService.save(logEntity);
    }

    /**
     * 初始化Log对象
     * @param log log注解
     * @param methodName 方法名
     * @return LogEntity对象
     */
    private LogEntity initLog(Log log, String methodName) {
        LogEntity logEntity = new LogEntity();
        logEntity.setDate(new Date());
        logEntity.setMethods(methodName);
        logEntity.setType(log.type().getCode());
        logEntity.setModule(log.module());
        logEntity.setContent(log.content());
        logEntity.setActionUrl(log.actionUrl());
        return logEntity;
    }

    /**
     * 获取目标类的所有方法,找到当前要执行的方法
     * @param joinPoint  连接点
     * @param methodName 方法名称
     * @return 方法
     */
    private Method currentMethod(JoinPoint joinPoint, String methodName) {
        Method[] methods = joinPoint.getTarget().getClass().getMethods();
        Method resultMethod = null;
        for (Method method : methods) {
            if (method.getName().equals(methodName)) {
                resultMethod = method;
                break;
            }
        }
        return resultMethod;
    }
}

4.编写日志操作类型的枚举

import lombok.Getter;

/**
 * @Description: TODO
 * @Author yjw
 * @Date 2020/4/3
 **/
@Getter
public enum MethodTypeEnum {

    INSERT(1, "新增"),
    UPDATE(2, "修改"),
    DELETE(3, "删除"),
    SELECT(4, "查询")
    ;

    MethodTypeEnum(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    /**
     * 状态值
     */
    private int code;
    /**
     * 描述信息
     */
    private String msg;
}

5.在方法上增加注解

    /**
     * 查询所有
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询所有")
    @Log(module = "用户", content = "adsfa", actionUrl = "/a/q/", type = MethodTypeEnum.SELECT)
    public CarpResult<List<UserFeedbackEntity>> list() {
        List<UserFeedbackEntity> userFeedbackList = this.userFeedbackService.list();
        return new CarpResult<List<UserFeedbackEntity>>(ResultEnum.SUCCESS_CODE.getCode(), ResultEnum.SUCCESS_CODE.getMsg(), userFeedbackList);
    }

6.测试类测试

import com.cdls.carp.business.annotation.Log;
import com.cdls.carp.business.controller.UserFeedbackController;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * @Description: TODO
 * @Author yjw
 * @Date 2020/4/3
 **/
@SpringBootTest(classes = CarpBusinessApplication.class)
@RunWith(SpringRunner.class)
public class CarpTest {

    @Autowired
    UserFeedbackController userFeedbackController;

    @Test
    public void list() {
        userFeedbackController.list();
    }

}

成功

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值