【在线OJ系统】自定义注解实现分布式ID无感自增

news/2025/3/15 5:15:36/
实现思路

首先自定义参数注解,然后根据AOP思想,找到该注解作用的切点,也就是mapper层对于mapper层的接口在执行前都会执行该aop操作:获取到对于的方法对象,根据方法对象获取参数列表,根据参数列表判断某个参数上是否加有自定义的注解,如果有则读取注解中的value值,并通过传入对象的引用拿到该对象,此时判断该对象的id值是否位空,如果为空则生成全局唯一id并赋值

代码实现
package com.cloud.cloud_oj_learn.aspects;import com.cloud.cloud_oj_common.annotations.GenerateId;
import com.cloud.cloud_oj_common.enums.Keys;
import jakarta.annotation.Resource;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;/*** Created with IntelliJ IDEA.* Description:* User: PG* Date: 2024-04-16* Time: 18:49*/
@Aspect
@Component
public class GenerateIdAspect {@Resourceprivate StringRedisTemplate stringRedisTemplate;// 初始时间戳private static final long BEGIN_TIME = 1640995200L;// 序列化位数private static final int BITE = 32;public Long getNext(String tableName) {// 1.生成时间戳LocalDateTime now = LocalDateTime.now();long nowSecond = now.toEpochSecond(ZoneOffset.UTC);long timestamp = nowSecond - BEGIN_TIME;// 2,生成序列化号String date = now.format(DateTimeFormatter.ofPattern("yyyy:MM:dd"));long count = stringRedisTemplate.opsForValue().increment(Keys.GENERATE_ID.getKey() + tableName + ":" + date);// 3.拼接  返回return Long.valueOf(timestamp << BITE | count);}@Before("execution(* com.cloud.cloud_oj_learn.mapper.*.*(..))")public void before(JoinPoint joinPoint) {// 获取方法签名MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();// 获取方法对象Method method = methodSignature.getMethod();// 获取方法参数值数组Object[] args = joinPoint.getArgs();// 获取方法注解Annotation[][] parameterAnnotations = method.getParameterAnnotations();for (int i = 0; i < parameterAnnotations.length; i++) {for (Annotation annotation : parameterAnnotations[i]) {if (annotation instanceof GenerateId) {try {// 获取参数对象Object arg = args[i];// 获取这个对象的类文件Class<?> clazz = arg.getClass();// 获取这个对象的id属性Field idField = clazz.getDeclaredField("id");// 设置字段修改权限idField.setAccessible(true);// 获取注解上的value值String value = ((GenerateId) annotation).value();// 获取该value对应的下一个idLong nextId = getNext(value);// 判断是否为空, 如果为空就赋值 if (idField.get(arg) == null) {idField.set(arg, nextId);}} catch (NoSuchFieldException | IllegalAccessException e) {throw new RuntimeException(e);}}}}}}

图示


http://www.ppmy.cn/news/1426814.html

相关文章

python应用

划分数据集 将数据集划分为训练集和测试集 import os import shutil import random # 数据集路径 images_dir ./images targets_dir ./gt # 划分后的路径 train_images_dir ./train/images test_images_dir ./test/images train_targets_dir ./train/gt test_ta…

Jackson 2.x 系列【25】Spring Boot 集成之起步依赖、自动配置

有道无术&#xff0c;术尚可求&#xff0c;有术无道&#xff0c;止于术。 本系列Jackson 版本 2.17.0 本系列Spring Boot 版本 3.2.4 源码地址&#xff1a;https://gitee.com/pearl-organization/study-jaskson-demo 文章目录 1. 前言2. 起步依赖3. 自动配置3.1 JacksonPrope…

软考139-上午题-【软件工程】-软件容错技术

一、软件容错技术 提高软件质量和可靠性的技术大致可分为两类&#xff0c;一类是避开错误&#xff0c;即在开发的过程中不让差错潜入软件的技术&#xff1b;另一类是容错技术&#xff0c;即对某些无法避开的差错&#xff0c;使其影响减至最小的技术。 1-1、容错软件的定义 归…

2024 年江苏省职业院校技能大赛“区块链技术应用” 赛项赛卷(样卷)运维题解析一

运维题 环境: ubuntu20 fisco 2.8.0 前言 准备两台机子,并且可以能相互pin通 192.168.19.133 [M1-A] 192.168.19.137 [M2-B] 子任务 1-2-1: 搭建区块链系统并验证 基于给定服务器环境以及软件,搭建一条双机 1 机构 8 节点 1 群组的区块 链系统(默认端口开始[30300,2020…

算法刷题记录 Day49

算法刷题记录 Day49 Date: 2024.04.17 lc 5. 最长回文子串 class Solution { public:string longestPalindrome(string s) {// 1. dp. dp[i][j]表示s[i:j]是否是回文子串。// dp[i][j] s[i] s[j] && dp[i1][j-1];int n s.size();vector<vector<bool>>…

【iOS】—— SDWebImage源码学习(1)(未完)

iOS】—— SDWebImage源码学习 SDWebImage基础使用主要功能获取图片缓存缓存机制 SDWebImage SDWebImage具有缓存支持的异步映像下载程序。并添加了像UI元素分类类UIImageView、UIButton、MKAnnotationView&#xff0c;可以直接为这些UI元素添加图片。 基础使用 在日常的使用中…

04 JavaScript学习:输出

JavaScript 没有任何打印或者输出的函数。 JavaScript 显示数据 JavaScript 可以通过不同的方式来输出数据&#xff1a; 使用 window.alert() 弹出警告框。使用 document.write() 方法将内容写到 HTML 文档中。使用 innerHTML 写入到 HTML 元素。使用 console.log() 写入到浏…

研究三门问题以及更多门问题换门概率

import randomdef play(n,change):door [i for i in range(0,n)] # 0-n-1代表 n 个门has_car [0 for _ in range(0,n)]# 生成一个答案true_answer random.choice(door)has_car[true_answer] 1# 获取参赛者的答案your_answer random.choice(door)if has_car[your_answer]…