针对某个自定义注解进行aop拦截
Posted IT的鱼
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了针对某个自定义注解进行aop拦截相关的知识,希望对你有一定的参考价值。
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface AvoidResubmit {
/**
* 失效时间
*
* @return s
*/
long expireTime() default 5 * 1000L;
}
@Component
@Aspect
@Slf4j
public class AvoidResubmitAspect {
@Resource
private RedisTemplate<String, Object> redisTemplate;
@Inject
private IRegistryQueryService iRegistryQueryService;
@Value("${spring.profiles.active}")
String profile;
@Pointcut(value = "@annotation(com.huawei.it.util.resubmit.AvoidResubmit)")
private void submit(){
}
@Before("submit()&&@annotation(avoidReSubmit)")
private void doBefore(JoinPoint joinPoint, AvoidResubmit avoidReSubmit) throws Exception {
// 如果开启了数据字典就打卡
String resubmit = iRegistryQueryService.findValueByPath("App.System.reSubmit", true);
if(StringUtils.isNotEmpty(resubmit) && "1".equals(resubmit)) {
// 如果参数为空,则加入方法
StringBuilder key = new StringBuilder(150);
key.append("resubmit_");
key.append(AppContextUtil.getContexW3Account()).append("_");
key.append(profile).append("_");
key.append(joinPoint.getSignature().toShortString());
long expireTime = avoidReSubmit.expireTime();
ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue();
Object object = valueOperations.get(key.toString());
if (null != object) {
log.error(key.toString() + "_resubmit");
throw new BizApplicationException("请求已提交请求,请不要重复提交");
}
valueOperations.set(key.toString(), 1, expireTime, TimeUnit.MILLISECONDS);
}
}
}
以上是关于针对某个自定义注解进行aop拦截的主要内容,如果未能解决你的问题,请参考以下文章