Spring boot 添加 AOP【打印访问日志,防止重复访问】

@Aspect
@Component
@Slf4j
public class ControllerConfig {

    @Autowired
    private RedissonService redissonService;

    @Pointcut(value = "execution(public * com.*.controller.*.*(..) )")
    private void point() {
    }

    @Around("point()")
    public Object pointcut(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        long startTime = System.currentTimeMillis();

        String methodName = proceedingJoinPoint.getSignature().getName();
        String className = proceedingJoinPoint.getTarget().getClass().getName();
        //参数列表
        Object[] args = proceedingJoinPoint.getArgs();

        StringBuilder sb = new StringBuilder();
        Arrays.asList(args).forEach((o) -> {
            if (!(o instanceof ServletRequest) && !(o instanceof MultipartFile) && !(o instanceof ServletResponse)) {
                sb.append(JSON.toJSONString(o) + ",");
            }
        });

        //获取参数名
        String[] argNames = ((MethodSignature) proceedingJoinPoint.getSignature()).getParameterNames();

        String userId = null;
        for (int i = 0; i < argNames.length; i++) {
            if ("userId".equals(argNames[i])) {
                userId = String.valueOf(args[i]);
            }
        }

        log.info("执行->[{}]类的->[{}]方法的请求参数为:->[{}]", className, methodName, sb.toString());

        Object proceed = null;
        //处理重复提交
        if (userId != null) {   
            String key = new StringBuilder("redisLock").append(className).append("-").append(methodName).append("-").append(userId).toString();
            RLock lock = redissonService.getRLock(key);
            try {
                lock.lock();
                proceed = proceedingJoinPoint.proceed();
            } catch (Exception e) {
                log.error("系统异常,异常信息:{}", e);
                proceed = R.error();
            } finally {
                lock.unlock();
            }

        } else {
            proceed = proceedingJoinPoint.proceed();
        }

        long endTime = System.currentTimeMillis();
        log.info("执行->[{}]类的[{}]方法的返回报文为:->[{}],执行时长为:[{}] ms", className, methodName, JSON.toJSONString(proceed), (endTime - startTime));
        return proceed;
    }
}

    org.redisson
    redisson
    3.5.7

你可能感兴趣的:(Spring,Boot)