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拦截内容请搜索快网idc以前的文章或继续浏览下面的相关文章希望大家以后多多支持快网idc!
原文链接:https://www.cnblogs.com/lori/archive/2020/08/19/13528403.html
相关文章
猜你喜欢
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-29 71
-
2025-05-29 29
-
2025-05-29 89
-
2025-06-04 78
-
2025-05-25 54
热门评论