Skip to content

Commit 589669d

Browse files
committed
Polish "Add support for selecting the Redis client to use"
See gh-22569
1 parent ac65144 commit 589669d

File tree

6 files changed

+35
-25
lines changed

6 files changed

+35
-25
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/JedisConnectionConfiguration.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -43,7 +43,6 @@
4343
*
4444
* @author Mark Paluch
4545
* @author Stephane Nicoll
46-
* @author Chris Bono
4746
*/
4847
@Configuration(proxyBeanMethods = false)
4948
@ConditionalOnClass({ GenericObjectPool.class, JedisConnection.class, Jedis.class })

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/LettuceConnectionConfiguration.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
*
5151
* @author Mark Paluch
5252
* @author Andy Wilkinson
53-
* @author Chris Bono
5453
*/
5554
@Configuration(proxyBeanMethods = false)
5655
@ConditionalOnClass(RedisClient.class)

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/RedisAutoConfiguration.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@
2121
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2222
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2323
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
24+
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
2425
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2526
import org.springframework.context.annotation.Bean;
2627
import org.springframework.context.annotation.Configuration;
@@ -52,6 +53,7 @@ public class RedisAutoConfiguration {
5253

5354
@Bean
5455
@ConditionalOnMissingBean(name = "redisTemplate")
56+
@ConditionalOnSingleCandidate(RedisConnectionFactory.class)
5557
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory)
5658
throws UnknownHostException {
5759
RedisTemplate<Object, Object> template = new RedisTemplate<>();
@@ -61,6 +63,7 @@ public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisC
6163

6264
@Bean
6365
@ConditionalOnMissingBean
66+
@ConditionalOnSingleCandidate(RedisConnectionFactory.class)
6467
public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisConnectionFactory)
6568
throws UnknownHostException {
6669
StringRedisTemplate template = new StringRedisTemplate();

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/RedisProperties.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ public class RedisProperties {
7777
private String clientName;
7878

7979
/**
80-
* Type of client to use.
80+
* Type of client to use. By default, auto-detected according to the classpath.
8181
*/
82-
private ClientType clientType = ClientType.Lettuce;
82+
private ClientType clientType;
8383

8484
private Sentinel sentinel;
8585

@@ -191,14 +191,15 @@ public Lettuce getLettuce() {
191191
public enum ClientType {
192192

193193
/**
194-
* Use the Lettuce client
194+
* Use the Lettuce redis client.
195195
*/
196-
Lettuce,
196+
LETTUCE,
197197

198198
/**
199-
* Use the Jedis client
199+
* Use the Jedis redis client.
200200
*/
201-
Jedis
201+
JEDIS
202+
202203
}
203204

204205
/**

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/redis/RedisAutoConfigurationJedisTests.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,6 +25,7 @@
2525
import org.springframework.boot.testsupport.classpath.ClassPathExclusions;
2626
import org.springframework.context.annotation.Bean;
2727
import org.springframework.context.annotation.Configuration;
28+
import org.springframework.data.redis.connection.RedisConnectionFactory;
2829
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration.JedisClientConfigurationBuilder;
2930
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
3031

@@ -43,11 +44,17 @@ class RedisAutoConfigurationJedisTests {
4344
.withConfiguration(AutoConfigurations.of(RedisAutoConfiguration.class));
4445

4546
@Test
46-
void testDefaultRedisConfiguration() {
47+
void connectionFactoryDefaultsToJedis() {
4748
this.contextRunner.run((context) -> assertThat(context.getBean("redisConnectionFactory"))
4849
.isInstanceOf(JedisConnectionFactory.class));
4950
}
5051

52+
@Test
53+
void connectionFactoryIsNotCreatedWhenLettuceIsSelected() {
54+
this.contextRunner.withPropertyValues("spring.redis.client-type=lettuce")
55+
.run((context) -> assertThat(context).doesNotHaveBean(RedisConnectionFactory.class));
56+
}
57+
5158
@Test
5259
void testOverrideRedisConfiguration() {
5360
this.contextRunner.withPropertyValues("spring.redis.host:foo", "spring.redis.database:1").run((context) -> {

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/redis/RedisAutoConfigurationTests.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.springframework.context.annotation.Bean;
3838
import org.springframework.context.annotation.Configuration;
3939
import org.springframework.data.redis.connection.RedisClusterConfiguration;
40+
import org.springframework.data.redis.connection.RedisConnectionFactory;
4041
import org.springframework.data.redis.connection.RedisNode;
4142
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
4243
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
@@ -53,8 +54,7 @@
5354
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
5455

5556
/**
56-
* Tests for {@link RedisAutoConfiguration} when both Jedis and Lettuce are on the
57-
* classpath.
57+
* Tests for {@link RedisAutoConfiguration}.
5858
*
5959
* @author Dave Syer
6060
* @author Christian Dupuis
@@ -74,10 +74,10 @@ class RedisAutoConfigurationTests {
7474
@Test
7575
void testDefaultRedisConfiguration() {
7676
this.contextRunner.run((context) -> {
77-
assertThat(context.getBean("redisTemplate", RedisOperations.class)).isNotNull();
78-
assertThat(context.getBean(StringRedisTemplate.class)).isNotNull();
79-
assertThat(context.getBean("redisConnectionFactory")).isInstanceOf(LettuceConnectionFactory.class);
80-
assertThat(context.getBeanProvider(JedisConnectionConfiguration.class).getIfAvailable()).isNull();
77+
assertThat(context.getBean("redisTemplate")).isInstanceOf(RedisOperations.class);
78+
assertThat(context).hasSingleBean(StringRedisTemplate.class);
79+
assertThat(context).hasSingleBean(RedisConnectionFactory.class);
80+
assertThat(context.getBean(RedisConnectionFactory.class)).isInstanceOf(LettuceConnectionFactory.class);
8181
});
8282
}
8383

@@ -187,17 +187,18 @@ void testRedisConfigurationWithClientName() {
187187
}
188188

189189
@Test
190-
void testRedisConfigurationWithClientNameJedis() {
191-
this.contextRunner.withPropertyValues("spring.redis.client-type:jedis")
192-
.run((context) -> assertThat(context.getBean("redisConnectionFactory"))
193-
.isInstanceOf(JedisConnectionFactory.class));
190+
void connectionFactoryWithJedisClientType() {
191+
this.contextRunner.withPropertyValues("spring.redis.client-type:jedis").run((context) -> {
192+
assertThat(context).hasSingleBean(RedisConnectionFactory.class);
193+
assertThat(context.getBean(RedisConnectionFactory.class)).isInstanceOf(JedisConnectionFactory.class);
194+
});
194195
}
195196

196197
@Test
197-
void testRedisConfigurationWithClientNameLettuce() {
198+
void connectionFactoryWithLettuceClientType() {
198199
this.contextRunner.withPropertyValues("spring.redis.client-type:lettuce").run((context) -> {
199-
assertThat(context.getBean("redisConnectionFactory")).isInstanceOf(LettuceConnectionFactory.class);
200-
assertThat(context.getBeanProvider(JedisConnectionConfiguration.class).getIfAvailable()).isNull();
200+
assertThat(context).hasSingleBean(RedisConnectionFactory.class);
201+
assertThat(context.getBean(RedisConnectionFactory.class)).isInstanceOf(LettuceConnectionFactory.class);
201202
});
202203
}
203204

0 commit comments

Comments
 (0)