SpringBoot 防止表单重复提交-本地锁(11)
2021-06-06 18:04
阅读:736
YPE html>
标签:ext localhost round eth query 本地 直接 str i++
一、导入 maven 坐标依赖
org.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-aopcom.google.guava guava21.0
二、项目核心代码
新建 LocalLock 注解
package cn.hanzhenya.anno;
import java.lang.annotation.*;
/**
* @Classname LocalLock
* @Description 防止重复提交的注解
* @Date 2020/10/11 10:23
* @Created by hzy
* @Version 1.0
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface LocalLock {
String key() default "";
/**
* TODO 由于用的 guava 暂时就忽略该属性吧 集成 redis 需要用到
* 默认时间5秒
*/
int expire() default 5 * 1000;
}
配置拦截 + AOP
package cn.hanzhenya.interceptor;
import cn.gorit.anno.LocalLock;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;
/**
* @Classname LockMethodInterceptor
* @Description TODO
* @Date 2020/10/12 19:50
* @Created by hzy
* @Version 1.0
*/
@Aspect
@Configuration
public class LockMethodInterceptor {
// 构建缓存对象,设置过期时间
private static final Cache CACHES = CacheBuilder.newBuilder()
// 最大缓存 100 个
.maximumSize(1000)
// 设置写缓存后 5 秒钟过期
.expireAfterWrite(5, TimeUnit.SECONDS)
.build();
// 在 interceptor() 上采用 环绕通知,以及指定 @annotation 所有带 LocalLock 注解都将被切面处理
@Around("execution(public * *(..)) && @annotation(cn.gorit.anno.LocalLock)")
public Object interceptor(ProceedingJoinPoint pjp) {
MethodSignature signature = (MethodSignature) pjp.getSignature();
Method method = signature.getMethod();
LocalLock localLock = method.getAnnotation(LocalLock.class);
String key = getKey(localLock.key(), pjp.getArgs());
if (!StringUtils.isEmpty(key)) {
if (CACHES.getIfPresent(key) != null) {
throw new RuntimeException("请勿重复请求");
}
// 如果是第一次请求,就将 key 当前对象压入缓存中
CACHES.put(key, key);
}
try {
return pjp.proceed();
} catch (Throwable throwable) {
// 这里我使用到了全局异常处理,所以控制台不会直接报错
throw new RuntimeException("服务器异常");
} finally {
// TODO 为了演示效果,这里就不调用 CACHES.invalidate(key); 代码了
}
}
/**
* key 的生成策略,如果想灵活可以写成接口与实现类的方式(TODO 后续讲解)
*
* @param keyExpress 表达式
* @param args 参数
* @return 生成的key
*/
private String getKey(String keyExpress, Object[] args) {
for (int i = 0; i
控制层
// 这里模拟增删改查,就不用数据库了
@RestController
public class FirstController {
static List list = new ArrayList();
{
// 初始化数据,没有用到数据库,所有数据都是模拟的
list.add(new User(1,"牛二",13));
list.add(new User(2,"张三",14));
list.add(new User(3,"王五",15));
}
/**
* 查询所有的用户
* @return
*/
@RequestMapping(value = "/user",method = RequestMethod.GET)
public Result queryAllUsers() {
// Result 也是自定义的一个实体类
return new Result(200,"查询成功",list);
}
/**
* 根据 ID 查询用户
*/
@RequestMapping(value = "/user/{id}",method = RequestMethod.GET)
public Result queryUserById(@PathVariable("id") Integer id) {
return new Result(200,"查询成功",list.get(id-1));
}
/**
* 添加用户,返回添加的结果
*/
@LocalLock(key = "user:arg[0]")
@RequestMapping(value = "/user",method = RequestMethod.POST)
public Result addUser(User user) {
list.add(user);
return new Result(200,"添加成功",list);
}
前端页面
Title 查询所有用户
查询所有用户根据 ID 查询用户
请输入用户ID:添加用户
SpringBoot 防止表单重复提交-本地锁(11)
标签:ext localhost round eth query 本地 直接 str i++
原文地址:https://www.cnblogs.com/h-z-y/p/14602366.html
上一篇:Python学习(13)——元组
下一篇:Spring 事务介绍
文章来自:搜素材网的编程语言模块,转载请注明文章出处。
文章标题:SpringBoot 防止表单重复提交-本地锁(11)
文章链接:http://soscw.com/index.php/essay/91391.html
文章标题:SpringBoot 防止表单重复提交-本地锁(11)
文章链接:http://soscw.com/index.php/essay/91391.html
评论
亲,登录后才可以留言!