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

作者:张占岭 时间:2023-10-15 20:12:54 

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
@Aspect
public class RepeatSubmitAspect {
 @Autowired
 StringRedisTemplate redisTemplate;

@Around("@annotation(repeatSubmit)")
 public Object around(ProceedingJoinPoint proceedingJoinPoint, RepeatSubmit repeatSubmit) throws Throwable {
   log.info("repeatSubmit={}", repeatSubmit.toString());
 }
}

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


@Component
public class RepeatSubmitController {
 @RepeatSubmit(key = "get")
 public String get() {
   return "success";
 }
}

测试代码


@RunWith(SpringRunner.class)
@SpringBootTest()
@Slf4j
public class RepeatSubmitTest {
 @Autowired
 RepeatSubmitController repeatSubmitController;

@Test
 public void test() {
   log.info(repeatSubmitController.get());
 }
}

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

来源:https://www.cnblogs.com/lori/archive/2020/08/19/13528403.html

标签:springboot,aspect,annotation,拦截
0
投稿

猜你喜欢

  • 在C#使用字典存储事件示例及实现自定义事件访问器

    2022-08-14 14:34:52
  • Android 通过productFlavors实现多渠道打包方法示例

    2022-08-27 16:04:35
  • Android编程简单实现雷达扫描效果

    2021-06-17 20:38:04
  • 关于eclipse安装spring插件报错An error occurred while collecting items to be installed...解决方案

    2023-05-27 03:34:45
  • Java中List集合去除重复数据的方法汇总

    2021-07-05 14:57:21
  • Android NDK开发(C语言-文件读写)

    2022-07-04 17:02:09
  • 深入了解Spring中的@Autowired和@Resource注解

    2021-09-19 06:57:20
  • Android实现左滑删除控件

    2023-03-02 21:36:49
  • C# Winform多屏幕多显示器编程技巧实例

    2021-09-19 16:49:09
  • C#模拟Http与Https请求框架类实例

    2023-02-10 16:28:11
  • Android 中在有序广播中添加自定义权限的实例

    2021-08-10 05:09:35
  • Android中oncreate中获得控件高度或宽度的实现方法

    2023-09-27 05:32:12
  • Java枚举类型enum的详解及使用

    2023-08-02 14:23:57
  • java对象拷贝常见面试题及应答汇总

    2022-01-08 00:01:52
  • Java并发编程之CountDownLatch源码解析

    2023-11-05 02:06:41
  • Spring Boot如何整合FreeMarker模板引擎

    2022-09-06 15:49:32
  • C#中的虚函数virtual

    2023-09-07 13:49:53
  • 深入理解java中的重载和覆盖

    2023-01-05 00:24:22
  • Android实现关机后数据不会丢失问题

    2021-06-08 18:54:47
  • C#实现XML文件操作详解

    2023-07-16 12:36:52
  • asp之家 软件编程 m.aspxhome.com