Redis与自定义注解实现重复

1、创建 SubmitLock 注解

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface SubmitLock {
    String key() default "";
}

2、注解实现


    @Autowired
    public RedisUtils redisUtils;

    @Around("execution(* com.example.code_generation..*Controller.*(..)) && @annotation(lock)")
    public Object submitInterceptor(ProceedingJoinPoint pjp, SubmitLock lock) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

        HttpServletRequest request = attributes.getRequest();

        String uri = request.getRequestURI();
        MethodSignature signature = (MethodSignature) pjp.getSignature();
        Method method = signature.getMethod();
        //真实类名字
        String targetName = pjp.getTarget().getClass().getName();
        //真实方式
        String methodName = pjp.getSignature().getName();
        //所有的请求参数
        Object[] arguments = pjp.getArgs();
        Object[] args = new Object[arguments.length];
        SubmitLock localLock = method.getAnnotation(SubmitLock.class);
        String key = setKey(localLock.key(), pjp.getArgs());

        if (!StringUtils.isEmpty(key)) {
            if (redisUtils.get(key) != null) {
                log.error("请勿重复操作,uri = 【{}】", uri);
                return new RspData<>(BizCodeEnum.WARN, "请勿重复操作");
            }
            redisUtils.set(key,key,2);
        }
        Object result = null;
        try {
            result = pjp.proceed();
            return result;
        }catch (Throwable throwable){
            throw new RuntimeException("服务器异常");
        }finally {
            int order = 0;
            for (Object arg : arguments){
                if (arg instanceof ServletRequest || arg instanceof ServletResponse || arg instanceof MultipartFile){
                    continue;
                }
                args[order] = arg;
                order ++ ;
            }
            log.info("调用Controller方法返回结果,targetName = {}, methodName = {}, args = {}, result = {}",
                    targetName, methodName, args, result);
        }

    }


    private String setKey(String keyExpress, Object[] args) {
        if (null != args && args.length > 0) {
            keyExpress = keyExpress.replace("arg[0]", args[0].toString());
        }
        return keyExpress;
    }

3、controller 测试验证

    @SubmitLock(key = "getTest")
    @ApiOperation(value = "获取代办任务")
    @GetMapping("getTest")
    public Object getTest(@CurrentUser UserInfo userInfo) {
        
    }

相关推荐

  1. Redis定义注解实现重复

    2024-02-06 05:50:03       33 阅读
  2. Spring定义注解+AOP实现接口防重复提交

    2024-02-06 05:50:03       13 阅读
  3. Spring Cloud项目如何防止重复提交(定义注解

    2024-02-06 05:50:03       36 阅读
  4. Springboot定义注解+aop实现redis自动清除缓存功能

    2024-02-06 05:50:03       15 阅读
  5. 基于AOP实现定义注解

    2024-02-06 05:50:03       14 阅读
  6. 定义注解+AOP实现日志记录

    2024-02-06 05:50:03       13 阅读
  7. 定义注解实现Excel 导出

    2024-02-06 05:50:03       6 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-06 05:50:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-06 05:50:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-06 05:50:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-06 05:50:03       18 阅读

热门阅读

  1. python 工厂函数举例

    2024-02-06 05:50:03       32 阅读
  2. gd32F470配置RTC时钟

    2024-02-06 05:50:03       31 阅读
  3. SpringBoot+Slf4j+Logback日志记录方案

    2024-02-06 05:50:03       31 阅读
  4. git的奇特知识点

    2024-02-06 05:50:03       29 阅读
  5. 各种编程语言送祝福:2024龙年大吉

    2024-02-06 05:50:03       31 阅读
  6. STM32_CAN调试模式解释:静默、回环、静默回环

    2024-02-06 05:50:03       36 阅读
  7. 使用Oracle数据库创建定时任务的方法

    2024-02-06 05:50:03       26 阅读
  8. 转换函数

    2024-02-06 05:50:03       31 阅读
  9. [Python进阶] 数据处理:Numpy入门

    2024-02-06 05:50:03       28 阅读
  10. 蓝桥杯刷题--python-1

    2024-02-06 05:50:03       39 阅读