springboot aspect通过@annotation进行拦截的实例代码详解

2025-05-29 0 97

annotation就是注解的意思,在我们使用的拦截器时,可以通过业务层添加的某个注解,对业务方法进行拦截,之前我们在进行统一方法拦截时使用的是execution,而注解的拦截我们使用@annotation即可,我们可以做个例子,比如搞个防止重复提交的注解,然后在拦截器里去写防止重复提交的逻辑就好了。

拦截器数据源

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23
/**

* 防止重复提交

*

* @author BD-PC220

*/

@Documented

@Retention(RetentionPolicy.RUNTIME)

@Target({ElementType.METHOD})

public @interface RepeatSubmit {

/**

* 间隔多长时间提交,默认1秒

*

* @return

*/

long time() default 1;

/**

* 作为验证重复提交的key,

*

* @return

*/

String key();

}

业务实现的拦截器代码

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15
/**

* URL重复提交拦截器.

*/

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

}

}

在单元测试里去建立业务方法,然后建立单元测试的方法等

?

1

2

3

4

5

6

7
@Component

public class RepeatSubmitController {

@RepeatSubmit(key = "get")

public String get() {

return "success";

}

}

测试代码

?

1

2

3

4

5

6

7

8

9

10

11

12
@RunWith(SpringRunner.class)

@SpringBootTest()

@Slf4j

public class RepeatSubmitTest {

@Autowired

RepeatSubmitController repeatSubmitController;

@Test

public void test() {

log.info(repeatSubmitController.get());

}

}

springboot aspect通过@annotation进行拦截的实例代码详解

到此这篇关于springboot aspect通过@annotation进行拦截的文章就介绍到这了,更多相关springboot aspect通过@annotation拦截内容请搜索快网idc以前的文章或继续浏览下面的相关文章希望大家以后多多支持快网idc!

原文链接:https://www.cnblogs.com/lori/archive/2020/08/19/13528403.html

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 springboot aspect通过@annotation进行拦截的实例代码详解 https://www.kuaiidc.com/118381.html

相关文章

发表评论
暂无评论