redisson-spring-boot-starter + springboot 3.1 配置 AutoConfig
时间: 2023-07-29 20:11:23 浏览: 215
首先,确保已经添加了 `redisson-spring-boot-starter` 依赖。
然后,在 Spring Boot 3.1 中,你可以使用 `@ConfigurationPropertiesScan` 来扫描自定义的配置类。因此,我们可以创建一个 `RedissonProperties` 配置类,并使用 `@ConfigurationProperties` 注解来指定属性前缀,如下所示:
```java
@ConfigurationProperties(prefix = "redisson")
public class RedissonProperties {
private String address;
private String password;
// 其他属性...
// getter 和 setter 方法...
}
```
接下来,我们可以创建一个配置类 `RedissonAutoConfiguration`,并使用 `@ConditionalOnClass` 注解来确保 Redisson 相关的类在类路径中存在。然后,我们可以使用 `@EnableConfigurationProperties` 注解来启用 `RedissonProperties` 配置属性,并使用 `@Bean` 注解来创建 `RedissonClient` 对象,如下所示:
```java
@Configuration
@ConditionalOnClass(Redisson.class)
@EnableConfigurationProperties(RedissonProperties.class)
public class RedissonAutoConfiguration {
@Bean(destroyMethod = "shutdown")
RedissonClient redissonClient(RedissonProperties redissonProperties) {
Config config = new Config();
config.useSingleServer()
.setAddress(redissonProperties.getAddress())
.setPassword(redissonProperties.getPassword());
// 其他配置...
return Redisson.create(config);
}
}
```
最后,我们需要在 `application.properties`(或 `application.yml`)文件中配置 Redisson 相关的属性,如下所示:
```properties
redisson.address=redis://localhost:6379
redisson.password=password
# 其他属性...
```
现在,我们就可以通过 `@Autowired` 注解来注入 `RedissonClient` 对象,并使用 Redisson 来操作 Redis 了。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)