当前位置: 首页 > news >正文

在非Spring Boot的Spring项目中使用Lock4j

在非Spring Boot的Spring项目中使用Lock4j

Lock4j虽然主要面向Spring Boot项目,但也可以在传统的Spring项目中使用。以下是配置和使用步骤:

1. 添加依赖

<dependency><groupId>com.baomidou</groupId><artifactId>lock4j-core</artifactId><version>最新版本</version>
</dependency><!-- 根据你选择的锁实现添加对应依赖 -->
<!-- 使用Redisson实现 -->
<dependency><groupId>org.redisson</groupId><artifactId>redisson</artifactId><version>3.16.1</version>
</dependency>

2. Spring XML配置方式

基础配置

<!-- 配置Redisson客户端 -->
<bean id="redissonClient" class="org.redisson.api.Redisson" factory-method="create"><constructor-arg><bean class="org.redisson.config.Config"><property name="singleServerConfig"><bean class="org.redisson.config.SingleServerConfig"><property name="address" value="redis://127.0.0.1:6379"/></bean></property></bean></constructor-arg>
</bean><!-- 配置Lock4j -->
<bean id="lockTemplate" class="com.baomidou.lock.spring.LockTemplate"><property name="lockExecutor" ref="redissonLockExecutor"/>
</bean><bean id="redissonLockExecutor" class="com.baomidou.lock.redisson.RedissonLockExecutor"><property name="redissonClient" ref="redissonClient"/>
</bean><!-- 启用Lock4j注解支持 -->
<bean class="com.baomidou.lock.spring.Lock4jAspect"/>

属性配置

在properties文件中添加:

lock4j.acquire-timeout=3000
lock4j.expire=30000

然后在XML中配置属性处理器:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations"><list><value>classpath:config.properties</value></list></property>
</bean><bean class="com.baomidou.lock.spring.Lock4jProperties"><property name="acquireTimeout" value="${lock4j.acquire-timeout}"/><property name="expire" value="${lock4j.expire}"/>
</bean>

3. 纯Java配置方式

如果你使用Java配置类:

@Configuration
@EnableAspectJAutoProxy
public class Lock4jConfig {@Beanpublic RedissonClient redissonClient() {Config config = new Config();config.useSingleServer().setAddress("redis://127.0.0.1:6379");return Redisson.create(config);}@Beanpublic Lock4jProperties lock4jProperties() {Lock4jProperties properties = new Lock4jProperties();properties.setAcquireTimeout(3000);properties.setExpire(30000);return properties;}@Beanpublic LockTemplate lockTemplate(RedissonClient redissonClient) {LockTemplate lockTemplate = new LockTemplate();lockTemplate.setLockExecutor(redissonLockExecutor(redissonClient));return lockTemplate;}@Beanpublic RedissonLockExecutor redissonLockExecutor(RedissonClient redissonClient) {RedissonLockExecutor executor = new RedissonLockExecutor();executor.setRedissonClient(redissonClient);return executor;}@Beanpublic Lock4jAspect lock4jAspect() {return new Lock4jAspect();}
}

4. 使用方式

注解方式使用

@Service
public class OrderService {@Lock4j(keys = {"#orderId"})public void processOrder(String orderId) {// 业务逻辑}
}

编程式使用

@Service
public class OrderService {@Autowiredprivate LockTemplate lockTemplate;public void processOrder(String orderId) {try {if (lockTemplate.lock("order:" + orderId, 3000, 30000)) {// 获取锁成功,执行业务逻辑} else {// 获取锁失败处理}} finally {lockTemplate.releaseLock("order:" + orderId);}}
}

5. 注意事项

  1. 确保Spring的AOP配置正确,以便@Lock4j注解能够正常工作
  2. 如果使用XML配置,确保<aop:aspectj-autoproxy/>已配置
  3. 锁的key设计要合理,避免不同业务间的冲突
  4. 在非Spring Boot环境中,所有配置都需要手动完成
  5. 确保Redisson或其他锁实现的连接配置正确

6. 自定义扩展

如果需要自定义锁失败处理,可以实现LockFailureStrategy接口:

public class CustomLockFailureStrategy implements LockFailureStrategy {@Overridepublic void onLockFailure(String key, long acquireTimeout, int acquireCount) {// 自定义锁失败处理逻辑throw new RuntimeException("获取锁失败,key: " + key);}
}

然后在配置中注册:

<bean id="customLockFailureStrategy" class="com.your.package.CustomLockFailureStrategy"/><bean class="com.baomidou.lock.spring.Lock4jAspect"><property name="lockFailureStrategy" ref="customLockFailureStrategy"/>
</bean>

通过以上配置,你就可以在传统的Spring项目中使用Lock4j实现分布式锁功能了。

http://www.lryc.cn/news/593200.html

相关文章:

  • 用graphviz画一个关系图
  • 云服务器磁盘IO性能优化的测试与配置方法
  • 2025年7月19日,二维矩阵
  • 智能制造——解读39页汽车行业数字化工厂解决方案【附全文阅读】
  • 异世界历险之数据结构世界(二叉树-leetcode)
  • 国产电科金仓数据库:融合进化,智领未来
  • 【Unity3D实例-功能-移动】角色移动-通过WSAD(Rigidbody方式)
  • 架构探索笔记【1】
  • JavaScript空值安全深度指南
  • windows内核研究(驱动开发之内核编程)
  • Java无服务架构新范式:Spring Native与AWS Lambda冷启动深度优化
  • 【小沐学GIS】基于Rust绘制三维数字地球Earth(Rust、OpenGL、GIS)
  • C++STL系列之概述
  • OpenCV 官翻5 - 机器学习
  • 【web安全】万能密码
  • 物联网系统中的可视化大屏定义
  • UGUI 性能优化系列:第三篇——渲染与像素填充率优化
  • 小明记账簿焕新记:从单色到多彩的主题进化之路
  • 【Android】ListView与RecyclerView的基础使用
  • 安全隔离新选择:SiLM5768L系列 - 集成互锁功能的高速六通道数字隔离器
  • 从随机数值到特征检测器的学习与更新
  • 【Linux驱动-快速回顾】简单了解一下PinCtrl子系统:设备树如何被接解析与匹配
  • 大模型 Function Call 的实现步骤及示例详解
  • SpringBoot 3.0 挥别 spring.factories,拥抱云原生新纪元
  • Java机考题:815. 公交路线 图论BFS
  • 猎板:在 5G 与 AI 时代,印制线路板如何满足高性能需求
  • SQL Server和PostgreSQL填充因子
  • 数据结构与算法之美:拓扑排序
  • 小谈相机的学习过程
  • ROS2 通过相机确定物品坐标位置