在非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 {
@Bean
public RedissonClient redissonClient() {
Config config = new Config();
config.useSingleServer()
.setAddress("redis://127.0.0.1:6379");
return Redisson.create(config);
}
@Bean
public Lock4jProperties lock4jProperties() {
Lock4jProperties properties = new Lock4jProperties();
properties.setAcquireTimeout(3000);
properties.setExpire(30000);
return properties;
}
@Bean
public LockTemplate lockTemplate(RedissonClient redissonClient) {
LockTemplate lockTemplate = new LockTemplate();
lockTemplate.setLockExecutor(redissonLockExecutor(redissonClient));
return lockTemplate;
}
@Bean
public RedissonLockExecutor redissonLockExecutor(RedissonClient redissonClient) {
RedissonLockExecutor executor = new RedissonLockExecutor();
executor.setRedissonClient(redissonClient);
return executor;
}
@Bean
public Lock4jAspect lock4jAspect() {
return new Lock4jAspect();
}
}
4. 使用方式
注解方式使用
@Service
public class OrderService {
@Lock4j(keys = {"#orderId"})
public void processOrder(String orderId) {
// 业务逻辑
}
}
编程式使用
@Service
public class OrderService {
@Autowired
private LockTemplate lockTemplate;
public void processOrder(String orderId) {
try {
if (lockTemplate.lock("order:" + orderId, 3000, 30000)) {
// 获取锁成功,执行业务逻辑
} else {
// 获取锁失败处理
}
} finally {
lockTemplate.releaseLock("order:" + orderId);
}
}
}
5. 注意事项
- 确保Spring的AOP配置正确,以便@Lock4j注解能够正常工作
- 如果使用XML配置,确保
<aop:aspectj-autoproxy/>
已配置 - 锁的key设计要合理,避免不同业务间的冲突
- 在非Spring Boot环境中,所有配置都需要手动完成
- 确保Redisson或其他锁实现的连接配置正确
6. 自定义扩展
如果需要自定义锁失败处理,可以实现LockFailureStrategy
接口:
public class CustomLockFailureStrategy implements LockFailureStrategy {
@Override
public 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实现分布式锁功能了。