国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

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

瀏覽:130日期:2023-05-01 14:00:45

annotation就是注解的意思,在我們使用的攔截器時(shí),可以通過業(yè)務(wù)層添加的某個(gè)注解,對業(yè)務(wù)方法進(jìn)行攔截,之前我們在進(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@Aspectpublic class RepeatSubmitAspect { @Autowired StringRedisTemplate redisTemplate; @Around('@annotation(repeatSubmit)') public Object around(ProceedingJoinPoint proceedingJoinPoint, RepeatSubmit repeatSubmit) throws Throwable { log.info('repeatSubmit={}', repeatSubmit.toString()); }}

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

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

測試代碼

@RunWith(SpringRunner.class)@SpringBootTest()@Slf4jpublic class RepeatSubmitTest { @Autowired RepeatSubmitController repeatSubmitController; @Test public void test() { log.info(repeatSubmitController.get()); }}

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

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

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 平南县| 泗阳县| 辰溪县| 徐汇区| 开封县| 方正县| 长葛市| 邛崃市| 仪陇县| 东安县| 渑池县| 上林县| 宕昌县| 开化县| 北碚区| 台南市| 南涧| 塔城市| 萨嘎县| 波密县| 南澳县| 上林县| 鄂伦春自治旗| 黄梅县| 松潘县| 门头沟区| 乐昌市| 于田县| 肇东市| 玉山县| 青冈县| 南宫市| 襄垣县| 长兴县| 宜都市| 郸城县| 兴山县| 巴中市| 洪湖市| 余庆县| 阳春市|