springboot aspect通過@annotation進(jìn)行攔截的實(shí)例代碼詳解
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()); }}

到此這篇關(guān)于springboot aspect通過@annotation進(jìn)行攔截的文章就介紹到這了,更多相關(guān)springboot aspect通過@annotation攔截內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. IDEA 2020.1.2 安裝教程附破解教程詳解2. 利用ajax+php實(shí)現(xiàn)商品價(jià)格計(jì)算3. Java利用TCP協(xié)議實(shí)現(xiàn)客戶端與服務(wù)器通信(附通信源碼)4. 使用AJAX(包含正則表達(dá)式)驗(yàn)證用戶登錄的步驟5. JS圖片懶加載庫VueLazyLoad詳解6. Java實(shí)現(xiàn)的迷宮游戲7. Python 解決火狐瀏覽器不彈出下載框直接下載的問題8. django queryset相加和篩選教程9. Java PreparedStatement用法詳解10. Spring如何集成ibatis項(xiàng)目并實(shí)現(xiàn)dao層基類封裝

網(wǎng)公網(wǎng)安備