Home  >  Article  >  Web Front-end  >  How spring-aop prevents repeated submission of network requests

How spring-aop prevents repeated submission of network requests

坏嘻嘻
坏嘻嘻Original
2018-09-14 15:03:461827browse

The specific principle is very simple. Through the surround notification of spring-aop, when the request starts, the request parameters will be converted to verify whether they already exist. If they exist, an error will be reported. Otherwise, they will be stored and deleted after the request is completed.

The specific code is as follows:
1. Comment @interface

package com.yuntu.commons.intelligent.annotation;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;/**
 * Created by niuzy on 2018-09-13.
 */@Target({ElementType.METHOD})@Retention(RetentionPolicy.RUNTIME)public @interface NotDuplicate {}

2. Spring aspect and surrounding notification

import com.yuntu.commons.ServiceException;import com.yuntu.commons.intelligent.ExceptionConstants;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.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.stereotype.Component;import java.lang.reflect.Method;import java.util.Set;import java.util.concurrent.ConcurrentSkipListSet;/**
 * Created by niuzy on 2018-09-13.
 */@Aspect@Componentpublic class NotDuplicateAop {
    private Logger LOG = LoggerFactory.getLogger(NotDuplicateAop.class);    private static final Set<String> KEY =  new ConcurrentSkipListSet<>();    @Pointcut("@annotation(com.yuntu.commons.intelligent.annotation.NotDuplicate)")    public void duplicate() {
    }    /**
     * 对方法拦截后进行参数验证
     * @param pjp
     * @return
     * @throws Throwable
     */
    @Around("duplicate()")    public Object duplicate(ProceedingJoinPoint pjp) throws Throwable {
        MethodSignature msig = (MethodSignature) pjp.getSignature();
        Method currentMethod = pjp.getTarget().getClass().getMethod(msig.getName(), msig.getParameterTypes());        //拼接签名
        StringBuilder sb = new StringBuilder(currentMethod.toString());
        Object[] args = pjp.getArgs();        for (Object object : args) {            if(object != null){
                sb.append(object.getClass().toString());
                sb.append(object.toString());
            }
        }
        String sign = sb.toString();        boolean success = KEY.add(sign);        if(!success){            throw new ServiceException(ExceptionConstants.ILLEGAL_REQUEST_EXCEPTION,"该方法正在执行,不能重复请求");
        }        try {            return pjp.proceed();
        } finally {
            KEY.remove(sign);
        }

    }
}

3. Use:
In Add @NotDuplicate

to the required methods

Related recommendations:

Spring Management MongoDB

Mongodb Integration Spring Example

The above is the detailed content of How spring-aop prevents repeated submission of network requests. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn