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

springboot aspect通過@annotation進(jìn)行攔截的實(shí)例代碼詳解

 更新時(shí)間:2020年08月19日 12:38:40   作者:張占嶺  
這篇文章主要介紹了springboot aspect通過@annotation進(jìn)行攔截的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

annotation就是注解的意思,在我們使用的攔截器時(shí),可以通過業(yè)務(wù)層添加的某個(gè)注解,對(duì)業(yè)務(wù)方法進(jìn)行攔截,之前我們?cè)谶M(jìn)行統(tǒng)一方法攔截時(shí)使用的是execution,而注解的攔截我們使用@annotation即可,我們可以做個(gè)例子,比如搞個(gè)防止重復(fù)提交的注解,然后在攔截器里去寫防止重復(fù)提交的邏輯就好了。

攔截器數(shù)據(jù)源

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

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

業(yè)務(wù)實(shí)現(xiàn)的攔截器代碼

/**
 * URL重復(fù)提交攔截器.
 */
@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());
  }
}

在單元測(cè)試?yán)锶ソI(yè)務(wù)方法,然后建立單元測(cè)試的方法等

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

測(cè)試代碼

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

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

到此這篇關(guān)于springboot aspect通過@annotation進(jìn)行攔截的文章就介紹到這了,更多相關(guān)springboot aspect通過@annotation攔截內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論