Aop日志记录

AOP日志记录


package com.longshare.custom.widget.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;
/**
 * @Description: 日志操作类型
 * @Author: twelve
 * @Date: Create by 13:53 2022/4/17
 */
@Getter
@AllArgsConstructor
public enum LogType {
    BOARD_ADD,
    BOARD_DELETE,
    BOARD_UPDATE,
    COLUMN_ADD,
    COLUMN_DELETE,
    COLUMN_UPDATE,
    GROUP_ADD,
    GROUP_DELETE,
    GROUP_UPDATE,
    DEFAULT,

}
@Documented
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface BoardChangeLog {
    /**
     * 业务类型
     */
    @AliasFor("type")
    LogType value() default LogType.DEFAULT;

    /**
     * 代表记录log的类型
     */
    @AliasFor("value")
    LogType type() default LogType.DEFAULT;
}

/**
 * @Description: 面板操作日志记录切面
 * @Author: twelve
 * @Date: Create by 13:47 2022/4/17
 */
@Aspect
@Component
@Order(1)
@Slf4j
public class BoardDefChangeAspect {

    //将切点换成了自己定义的注解
    @Pointcut("@annotation(com.longshare.custom.widget.annotation.BoardChangeLog)")
    public void pointBoardChangeCut() {

    }

    @Around(value = "pointBoardChangeCut()")
    public Object aroundMethod(ProceedingJoinPoint joinPoint) {
        try {
            Object arg = getBoardId(joinPoint, "boardId");
            String boardId = arg == null ? null : (String) arg;
            BoardDefinitionEntity oldBoardEntity = null;

            if (!StringUtils.isEmpty(boardId)) {
                oldBoardEntity = boardDefinitionMapper.selectByPrimaryKey(boardId);
            }
            Object result = joinPoint.proceed();
            // 获取被代理的方法
            MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
            Method method = methodSignature.getMethod();
            if (method != null) {
                ColumnChangeLog apiLog = AnnotatedElementUtils.findMergedAnnotation(method, ColumnChangeLog.class);
                switch (apiLog.value()) {
                    case COLUMN_DELETE: {

                        // 向数据库存入数据
                        .....
                        break;
                    }
                    case COLUMN_UPDATE: {

                        // 向数据库存入数据
                        ......
                        break;
                    }
                    case COLUMN_ADD: {
                        Optional<BoardDefinition> optional = (Optional<BoardDefinition>) result;
                        BoardDefinition newBoard = optional.orElse(null);
                        if (newBoard != null) {
                            // 向数据库存入数据
                            ......
                        }
                        break;
                    }
                }
            }
            //记录
            return result;
        } catch (Throwable throwable) {
            log.info("执行异常");
        }
        return null;

    }

    /**
     * 根据参数列表获取指定参数
     * @param joinPoint
     * @param arg
     * @return
     */
    private Object getBoardId(ProceedingJoinPoint joinPoint, String arg) {
        // 1. 获取方法中给的参数 前置
        Object[] args = joinPoint.getArgs();
        // 获取方法签名
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        // 在方法签名中获取所有参数的名称
        String[] parameterNames = methodSignature.getParameterNames();
        // 根据参数名称拿到下标, 参数值的数组和参数名称的数组下标是一一对应的
        int index = ArrayUtils.indexOf(parameterNames, arg);
        // 在参数数组中取出下标对应参数值
        return args[index];
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值