中文字幕精品亚洲无线码二区,国产黄a三级三级三级看三级,亚洲七七久久桃花影院,丰满少妇被猛烈进入,国产小视频在线观看网站

springboot~aspect通過@annotation進(jin)行攔(lan)截

annotation就是注解的意思,在我們使用的攔截器時,可以通過業務層添加的某個注解,對業務方法進行攔截,之前我們在進行統一方法攔截時使用的是execution,而注解(jie)的攔截我們使用@annotation即(ji)可,我們可以做個例(li)子,比如(ru)搞個防止重復提交(jiao)的注解(jie),然后在攔截器里(li)去寫防止重復提交(jiao)的邏輯就好了。

攔截器數據源

/**
 * 防止重復提交
 *
 * @author BD-PC220
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface RepeatSubmit {
    /**
     * 間隔多長時間提交,默認1秒
     *
     * @return
     */
    long time() default 1;

    /**
     * 作為驗證重復提交的key,
     *
     * @return
     */
    String key();
}

業務實現的攔截器代碼

/**
 * URL重復提交攔截器.
 */
@Slf4j
@Component
@Aspect
public class RepeatSubmitAspect {
    @Autowired
    StringRedisTemplate redisTemplate;

    @Around("@annotation(repeatSubmit)")
    public Object around(ProceedingJoinPoint proceedingJoinPoint, RepeatSubmit repeatSubmit) throws Throwable {
        log.info("repeatSubmit={}", repeatSubmit.toString());
    }
}

在單(dan)元(yuan)(yuan)測(ce)試(shi)里去(qu)建立(li)業務方(fang)法,然后建立(li)單(dan)元(yuan)(yuan)測(ce)試(shi)的方(fang)法等

@Component
public class RepeatSubmitController {
    @RepeatSubmit(key = "get")
    public String get() {
        return "success";
    }
}

測試代碼

@RunWith(SpringRunner.class)
@SpringBootTest()
@Slf4j
public class RepeatSubmitTest {
    @Autowired
    RepeatSubmitController repeatSubmitController;

    @Test
    public void test() {
        log.info(repeatSubmitController.get());
    }
}

研究

posted @ 2020-08-19 11:44  張占嶺  閱讀(2640)  評論(0)    收藏  舉報