莴苣或Jedis之间的Spring自动配置优先级

问题描述 投票:3回答:1

我想将Lettuce用作Redis客户端,这是spring-boot-starter-data-redis-active的默认依赖项。但是,我继承了Jedis作为另一个组件的依赖关系,而另一个组件是用纯Java代码编写的(没有Spring)。由于存在JedisConnectionFactory,因此在初始化LettuceConnectionFactory时会导致冲突。

如何确保Jedis依赖于其他组件,同时确保为我自己的代码初始化LettuceConnectionFactory?使用LetticeConnectionFactory的主要原因是我的服务中的反应式编程。这两个连接工厂都配置为通过RedisAutoConfiguration初始化,没有优先级选项。

https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/RedisAutoConfiguration.java

spring spring-boot redis jedis lettuce
1个回答
0
投票

您可以通过创建自己的@Configuration类并在其中添加新的@Bean来覆盖RedisConnectionFactory。

例如:

@Bean
RedisConnectionFactory myLettuceConnectionFactory() {
  // your setup....
  new LettuceConnectionFactory();
}

然后使用myLettuceConnectionFactory bean设置RedisTemplate @Bean

@Bean
public RedisTemplate<String, Object> redisTemplate() {
  final RedisTemplate<String, Object> template = new RedisTemplate<>();
  template.setConnectionFactory(myLettuceConnectionFactory());
  // other settings...
  return template;
}
© www.soinside.com 2019 - 2024. All rights reserved.