基于SpringBoo防止接口重复提交代码示例
立即下载
资源介绍:
基于SpringBoo防止接口重复提交代码示例
package com.example.demo2;
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.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;
@Aspect
@Component
public class LockAspect {
public static final Cache CACHES = CacheBuilder.newBuilder()
.maximumSize(50)
.expireAfterWrite(5, TimeUnit.SECONDS)
.build();
@Pointcut("@annotation(com.example.demo2.LockCommit)&&execution(* com.example.demo2.*.*(..))")
public void pointCut(){}
@Around("pointCut()")
public Object Lock(ProceedingJoinPoint joinPoint){
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
Method method = methodSignature.getMethod();
LockCommit lockCommit = method.getAnnotation(LockCommit.class);
String key = lockCommit.key();
if(key!=null &&!"".equals(key)){
if(CACHES.getIfPresent(key)!=null){
throw new RuntimeException("请勿重复提交");
}
CACHES.put(key,key);
}
Object object = null;
try {
object = joinPoint.proceed();
} catch (Throwable e) {
e.printStackTrace();
}
return object;
}
}