springboot aspect通過(guò)@annotation進(jìn)行攔截的實(shí)例代碼詳解
annotation就是注解的意思,在我們使用的攔截器時(shí),可以通過(guò)業(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 { /** * 間隔多長(zhǎng)時(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()); }}
在單元測(cè)試?yán)锶ソI(yè)務(wù)方法,然后建立單元測(cè)試的方法等
@Componentpublic class RepeatSubmitController { @RepeatSubmit(key = 'get') public String get() { return 'success'; }}
測(cè)試代碼
@RunWith(SpringRunner.class)@SpringBootTest()@Slf4jpublic class RepeatSubmitTest { @Autowired RepeatSubmitController repeatSubmitController; @Test public void test() { log.info(repeatSubmitController.get()); }}
到此這篇關(guān)于springboot aspect通過(guò)@annotation進(jìn)行攔截的文章就介紹到這了,更多相關(guān)springboot aspect通過(guò)@annotation攔截內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. 基于PHP做個(gè)圖片防盜鏈2. ASP.NET MVC把數(shù)據(jù)庫(kù)中枚舉項(xiàng)的數(shù)字轉(zhuǎn)換成文字3. asp.net core 認(rèn)證和授權(quán)實(shí)例詳解4. XML在語(yǔ)音合成中的應(yīng)用5. .NET中實(shí)現(xiàn)對(duì)象數(shù)據(jù)映射示例詳解6. 基于javaweb+jsp實(shí)現(xiàn)企業(yè)車輛管理系統(tǒng)7. ASP.NET MVC使用Boostrap實(shí)現(xiàn)產(chǎn)品展示、查詢、排序、分頁(yè)8. 如何使用ASP.NET Core 配置文件9. jscript與vbscript 操作XML元素屬性的代碼10. php使用正則驗(yàn)證密碼字段的復(fù)雜強(qiáng)度原理詳細(xì)講解 原創(chuàng)
