欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

spring 重復(fù)注解和aop攔截的實(shí)現(xiàn)示例

 更新時(shí)間:2021年08月25日 15:27:53   作者:花開浪漫拾  
本文主要介紹了spring 重復(fù)注解和aop攔截的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

前言:

  1:jdk1.8開始支持重復(fù)注解@Repeatable實(shí)現(xiàn)

  2:aop攔截需要攔截當(dāng)前注解和@Repeatable指向的包裝注解才可以完全攔截到,因?yàn)椋?.當(dāng)在在方法上只有一個(gè)注解時(shí),aop攔截認(rèn)為是非包裝類型注解。2.當(dāng)方法上有多個(gè)重復(fù)注解時(shí),aop攔截認(rèn)為是包裝類型注解。 

重復(fù)注解實(shí)現(xiàn)方式(RequestLimit為原始注解,RequestLimitPack為包裝注解):

import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 接口限流
 *
 * @author wulm
 */
@Repeatable(RequestLimit.RequestLimitPack.class)
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface RequestLimit {

    /**
     * 最大次數(shù),比如 每分鐘100次、每小時(shí)500次、每天1000次
     */
    int maxTimes();

    /**
     * 頻率更新時(shí)間,比如 60:每分鐘、  3600:每小時(shí)、  86400:每天
     **/
    int seconds();

    @Target({ElementType.METHOD})
    @Retention(RetentionPolicy.RUNTIME)
    @interface RequestLimitPack {
        RequestLimit[] value();
    }
}

重復(fù)注解效果:

 

aop攔截(設(shè)置了兩處@Around):

@Aspect
@Configuration
public class RequestLimitAop {
    private static final Logger LOGGER = LoggerFactory.getLogger(RequestLimitAop.class);

    public static final String REDIS_SPLIT_STR = "#";
    public static final String SUB_MODULE = "API";

    @Pointcut(value = "@annotation(com.zxy.product.hr.sync.web.config.annotation.RequestLimit)")
    public void pointcut() {
    }

    @Pointcut(value = "@annotation(com.zxy.product.hr.sync.web.config.annotation.RequestLimit" +
            ".RequestLimitPack)")
    public void pointcutRequestLimitOuts() {
    }


    @Around(value = "pointcut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        // 獲取攔截的方法名
        MethodSignature msig = (MethodSignature) joinPoint.getSignature();
        // 獲取到注解
        RequestLimit requestLimit = msig.getMethod().getAnnotation(RequestLimit.class);

        ResultInfo resultInfo = checkFrequency(requestLimit, false);
        if (ResultInfo.isSuccess(resultInfo)) {
            // 繼續(xù)執(zhí)行方法
            return joinPoint.proceed();
        } else {
            return resultInfo;
        }
    }

    @Around(value = "pointcutRequestLimitOuts()")
    public Object aroundRequestLimitOuts(ProceedingJoinPoint joinPoint) throws Throwable {
        // 獲取攔截的方法名
        MethodSignature msig = (MethodSignature) joinPoint.getSignature();
        // 獲取到注解
        RequestLimit.RequestLimitPack requestLimitPack = msig.getMethod()
                .getAnnotation(RequestLimit.RequestLimitPack.class);
        for (RequestLimit requestLimit : requestLimitPack.value()) {
            ResultInfo resultInfo = checkFrequency(requestLimit, false);
            if (!ResultInfo.isSuccess(resultInfo)) {
                //失敗立即返回
                return resultInfo;
            }
        }
        //沒問題則繼續(xù)執(zhí)行
        return joinPoint.proceed();
    }


    public static ResultInfo checkFrequency(RequestLimit requestLimit, boolean isInnerApi) {
     //代碼忽略......
    }

}



到此這篇關(guān)于spring 重復(fù)注解和aop攔截的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)spring 重復(fù)注解和aop攔截內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論