乐趣区

关于java:SpringBootAopredisJedis防止重复提交

增加依赖

<!--   增加 redis 依赖     -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
<!--   增加 aop 依赖     -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
<!--   增加 jedis 依赖     -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.8.2</version>
        </dependency>

配置 redis(application-redis.properties)

spring.redis.host=localhost
spring.redis.port=6379

redis.host=localhost
redis.port=6379

配置 redis(RedisConfig)

@Configuration
@PropertySource("classpath:/application-redis.properties")
public class RedisConfig {@Value("${redis.host}")
    private String host;
    @Value("${redis.port}")
    private Integer port;

    @Bean
    public Jedis jedis(){return new Jedis(host,port);
    }


}

自定义注解(NoRepeatSubmit)

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface NoRepeatSubmit {
    /**
     * 指定工夫内不可反复提交,单位毫秒秒
     * @return
     */
    int timeOut() default 3000;}

AOP(RepeatSubmit)

@Aspect
@Component
public class RepeatSubmit {@Around("@annotation(noRepeatSubmit)")
    public Object around(ProceedingJoinPoint joinPoint, NoRepeatSubmit noRepeatSubmit){

//        获取注解
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();

//        获取类,办法
        String className = method.getDeclaringClass().getName();
        String methodName = method.getName();

//        组装 key 用户惟一标识 + 操作和办法
        String key = "tokenKey" + className + methodName;

//        获取超时工夫
        int timeOut = noRepeatSubmit.timeOut();

//        创建对象 判断 key 值是否存在 无则抛出  有则增加 redis 缓存中 设置工夫生效
        Jedis jedis = new Jedis();
        if (jedis.exists(key)){System.out.println("请勿反复申请拦挡");
            throw new RuntimeException("请勿反复申请拦挡");
        }else{jedis.set(key,"申请胜利");
            System.out.println("申请胜利");
            jedis.pexpire(key,timeOut);
        }

        Object proceed = null;

        try {proceed = joinPoint.proceed();
        } catch (Throwable throwable) {throwable.printStackTrace();
        }
        return proceed;

    }

}

controller 应用

@RestController
public class MessageController {

    @CrossOrigin
    @DeleteMapping("deleteMessage")
    @NoRepeatSubmit(timeOut = 3000)
    public String deleteMessage(@RequestBody Message message){int i = messageService.deleteMusic(message.getId());
        if (i>=1){return "数据删除胜利";}else {return "数据删除失败,请重试!";}
    }

    @CrossOrigin
    @PostMapping("updateMessage")
    @NoRepeatSubmit(timeOut = 3000)
    public String updateMessage(@RequestBody Message message){if(message.getSex().equals("男")){message.setSex("1");
        }else if (message.getSex().equals("女")){message.setSex("2");
        }
        int i = messageService.updateMusic(message);
        if (i>=1){return "数据批改胜利";}else {return "数据批改失败,请重试!";}
    }

}
退出移动版