在非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 {

    @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. 注意事项

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值