AOP自定义注解防重

 Spring Boot 防重提交注解实现与实战示例

在Web开发中,防止用户重复提交表单是一个常见的需求。本文将详细介绍如何在Spring Boot中通过自定义注解和AOP技术实现防重提交功能,并提供一个完整的示例。

 一、背景介绍

重复提交问题通常出现在用户在短时间内多次点击提交按钮的场景,比如在提交表单或进行支付操作时。这种情况可能会导致数据错误或产生额外的费用。为了解决这个问题,我们可以使用分布式锁或基于Redis的防重机制。

 二、技术方案

本文将采用Redis和Spring AOP来实现防重提交注解。以下是技术方案的步骤:

1. 创建一个防重提交的注解。
2. 实现一个AOP切面,用于处理注解逻辑。
3. 在Controller中使用该注解。

 三、创建防重提交注解

首先,我们创建一个名为 @RepeatSubmitToken 的注解。

import java.lang.annotation.*;
import org.springframework.stereotype.Component;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Component
public @interface RepeatSubmitToken {
    // 可以添加一些属性,比如锁的过期时间等
}

 四、实现AOP切面

接下来,我们创建一个AOP切面RepeatSubmitAspect,用于处理防重逻辑。

import org.aspectj.lang.annotation.*;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;

@Aspect
@Component
public class RepeatSubmitAspect {

    private final StringRedisTemplate redisTemplate;

    public RepeatSubmitAspect(StringRedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    @Around("@annotation(repeatSubmitToken)")
    public Object around(ProceedingJoinPoint point, RepeatSubmitToken repeatSubmitToken) throws Throwable {
        Object[] args = point.getArgs();
        String key = "repeat_submit_token:" + args[0].toString();
        ValueOperations<String, String> ops = redisTemplate.opsForValue();

        boolean exists = ops.setIfAbsent(key, "true", 10, TimeUnit.SECONDS);
        if (!exists) {
            throw new RuntimeException("请勿重复提交");
        }
        try {
            return point.proceed();
        } finally {
            redisTemplate.delete(key);
        }
    }
}

 五、使用防重注解

现在,我们可以在Controller中使用`@RepeatSubmitToken`注解。

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {

    @PostMapping("/submitForm")
    @RepeatSubmitToken
    public String submitForm(String userId, String data) {
        // 处理表单提交逻辑
        return "提交成功";
    }
}

 六、测试与验证

部署应用后,我们可以通过POST请求测试接口,并观察是否能够正确防止重复提交。

1.手动设置锁,等待一秒后输出了 donothing.

2.aop枷锁

代码睡眠3秒,在一秒内多次请求,代码报错,后续输出正常的 do something

如果短时间内连续发送多次请求,应该会看到“请勿重复提交”的异常信息。

七、总结

通过自定义注解和AOP技术,我们可以在Spring Boot应用中轻松实现防重提交功能。这种方法不仅代码简洁,而且易于维护和扩展。

以上就是一个完整的Spring Boot防重提交注解的实现过程。希望这个示例能够帮助您在实际项目中解决重复提交的问题。

八、有兴趣的同学可以下载代码玩一下

redissonTest: 小草redisson分布式锁的例子,手动加锁和aop加锁,可以配合业务实现业务请求防重

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值