久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術文章
文章詳情頁

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

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

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

攔截器數據源

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

業務實現的攔截器代碼

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

在單元測試里去建立業務方法,然后建立單元測試的方法等

@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進行攔截的實例代碼詳解

到此這篇關于springboot aspect通過@annotation進行攔截的文章就介紹到這了,更多相關springboot aspect通過@annotation攔截內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
主站蜘蛛池模板: 汉阴县| 柯坪县| 淳安县| 拉萨市| 泊头市| 伊金霍洛旗| 苍山县| 望城县| 准格尔旗| 台安县| 麻栗坡县| 商水县| 临沧市| 霞浦县| 嵩明县| 姜堰市| 武夷山市| 普宁市| 宜良县| 紫阳县| 高青县| 巴青县| 凌海市| 万盛区| 乃东县| 舒兰市| 姜堰市| 大化| 阿拉善盟| 明星| 岱山县| 济阳县| 石狮市| 蒙阴县| 兴城市| 镇坪县| 纳雍县| 杨浦区| 和林格尔县| 运城市| 南华县|