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

springboot aspect通過@annotation進行攔截的實例代碼詳解

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

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

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

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

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

業(yè)務(wù)實現(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());
  }
}

在單元測試里去建立業(yè)務(wù)方法,然后建立單元測試的方法等

@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());
  }
}

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

相關(guān)文章

最新評論