Skip to content

Commit 236c2dd

Browse files
eddumelendezsnicoll
authored andcommitted
Add support for used-defined RedisStandaloneConfiguration
See gh-28028
1 parent 3e5038c commit 236c2dd

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.springframework.data.redis.connection.RedisClusterConfiguration;
3131
import org.springframework.data.redis.connection.RedisConnectionFactory;
3232
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
33+
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
3334
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
3435
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration.JedisClientConfigurationBuilder;
3536
import org.springframework.data.redis.connection.jedis.JedisConnection;
@@ -41,6 +42,7 @@
4142
*
4243
* @author Mark Paluch
4344
* @author Stephane Nicoll
45+
* @author Eddú Meléndez
4446
*/
4547
@Configuration(proxyBeanMethods = false)
4648
@ConditionalOnClass({ GenericObjectPool.class, JedisConnection.class, Jedis.class })
@@ -49,9 +51,10 @@
4951
class JedisConnectionConfiguration extends RedisConnectionConfiguration {
5052

5153
JedisConnectionConfiguration(RedisProperties properties,
54+
ObjectProvider<RedisStandaloneConfiguration> standaloneConfigurationProvider,
5255
ObjectProvider<RedisSentinelConfiguration> sentinelConfiguration,
5356
ObjectProvider<RedisClusterConfiguration> clusterConfiguration) {
54-
super(properties, sentinelConfiguration, clusterConfiguration);
57+
super(properties, standaloneConfigurationProvider, sentinelConfiguration, clusterConfiguration);
5558
}
5659

5760
@Bean

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.springframework.data.redis.connection.RedisClusterConfiguration;
4141
import org.springframework.data.redis.connection.RedisConnectionFactory;
4242
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
43+
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
4344
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
4445
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration.LettuceClientConfigurationBuilder;
4546
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
@@ -51,16 +52,18 @@
5152
*
5253
* @author Mark Paluch
5354
* @author Andy Wilkinson
55+
* @author Eddú Meléndez
5456
*/
5557
@Configuration(proxyBeanMethods = false)
5658
@ConditionalOnClass(RedisClient.class)
5759
@ConditionalOnProperty(name = "spring.redis.client-type", havingValue = "lettuce", matchIfMissing = true)
5860
class LettuceConnectionConfiguration extends RedisConnectionConfiguration {
5961

6062
LettuceConnectionConfiguration(RedisProperties properties,
63+
ObjectProvider<RedisStandaloneConfiguration> standaloneConfigurationProvider,
6164
ObjectProvider<RedisSentinelConfiguration> sentinelConfigurationProvider,
6265
ObjectProvider<RedisClusterConfiguration> clusterConfigurationProvider) {
63-
super(properties, sentinelConfigurationProvider, clusterConfigurationProvider);
66+
super(properties, standaloneConfigurationProvider, sentinelConfigurationProvider, clusterConfigurationProvider);
6467
}
6568

6669
@Bean(destroyMethod = "shutdown")

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
* @author Stephane Nicoll
4040
* @author Alen Turkovic
4141
* @author Scott Frederick
42+
* @author Eddú Meléndez
4243
*/
4344
abstract class RedisConnectionConfiguration {
4445

@@ -47,19 +48,26 @@ abstract class RedisConnectionConfiguration {
4748

4849
private final RedisProperties properties;
4950

51+
private final RedisStandaloneConfiguration standaloneConfiguration;
52+
5053
private final RedisSentinelConfiguration sentinelConfiguration;
5154

5255
private final RedisClusterConfiguration clusterConfiguration;
5356

5457
protected RedisConnectionConfiguration(RedisProperties properties,
58+
ObjectProvider<RedisStandaloneConfiguration> standaloneConfigurationProvider,
5559
ObjectProvider<RedisSentinelConfiguration> sentinelConfigurationProvider,
5660
ObjectProvider<RedisClusterConfiguration> clusterConfigurationProvider) {
5761
this.properties = properties;
62+
this.standaloneConfiguration = standaloneConfigurationProvider.getIfAvailable();
5863
this.sentinelConfiguration = sentinelConfigurationProvider.getIfAvailable();
5964
this.clusterConfiguration = clusterConfigurationProvider.getIfAvailable();
6065
}
6166

6267
protected final RedisStandaloneConfiguration getStandaloneConfig() {
68+
if (this.standaloneConfiguration != null) {
69+
return this.standaloneConfiguration;
70+
}
6371
RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
6472
if (StringUtils.hasText(this.properties.getUrl())) {
6573
ConnectionInfo connectionInfo = parseUrl(this.properties.getUrl());

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import org.springframework.data.redis.connection.RedisConnectionFactory;
4545
import org.springframework.data.redis.connection.RedisNode;
4646
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
47+
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
4748
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
4849
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
4950
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration.LettuceClientConfigurationBuilder;
@@ -235,6 +236,14 @@ void testRedisConfigurationWithDefaultTimeouts() {
235236
});
236237
}
237238

239+
@Test
240+
void testRedisConfigurationWithCustomBean() {
241+
this.contextRunner.withUserConfiguration(RedisStandaloneConfig.class).run((context) -> {
242+
LettuceConnectionFactory cf = context.getBean(LettuceConnectionFactory.class);
243+
assertThat(cf.getHostName()).isEqualTo("foo");
244+
});
245+
}
246+
238247
@Test
239248
void testRedisConfigurationWithClientName() {
240249
this.contextRunner.withPropertyValues("spring.redis.host:foo", "spring.redis.client-name:spring-boot")
@@ -456,4 +465,16 @@ LettuceClientConfigurationBuilderCustomizer customizer() {
456465

457466
}
458467

468+
@Configuration(proxyBeanMethods = false)
469+
static class RedisStandaloneConfig {
470+
471+
@Bean
472+
RedisStandaloneConfiguration standaloneConfiguration() {
473+
RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
474+
config.setHostName("foo");
475+
return config;
476+
}
477+
478+
}
479+
459480
}

0 commit comments

Comments
 (0)