Skip to content

Commit d06ebce

Browse files
committed
Merge pull request #28028 from eddumelendez
* pr/28028: Polish "Add support for used-defined RedisStandaloneConfiguration" Add support for used-defined RedisStandaloneConfiguration Closes gh-28028
2 parents 3e5038c + 124c3ac commit d06ebce

File tree

5 files changed

+36
-2
lines changed

5 files changed

+36
-2
lines changed

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

Lines changed: 3 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;
@@ -49,9 +50,10 @@
4950
class JedisConnectionConfiguration extends RedisConnectionConfiguration {
5051

5152
JedisConnectionConfiguration(RedisProperties properties,
53+
ObjectProvider<RedisStandaloneConfiguration> standaloneConfigurationProvider,
5254
ObjectProvider<RedisSentinelConfiguration> sentinelConfiguration,
5355
ObjectProvider<RedisClusterConfiguration> clusterConfiguration) {
54-
super(properties, sentinelConfiguration, clusterConfiguration);
56+
super(properties, standaloneConfigurationProvider, sentinelConfiguration, clusterConfiguration);
5557
}
5658

5759
@Bean

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

Lines changed: 3 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;
@@ -58,9 +59,10 @@
5859
class LettuceConnectionConfiguration extends RedisConnectionConfiguration {
5960

6061
LettuceConnectionConfiguration(RedisProperties properties,
62+
ObjectProvider<RedisStandaloneConfiguration> standaloneConfigurationProvider,
6163
ObjectProvider<RedisSentinelConfiguration> sentinelConfigurationProvider,
6264
ObjectProvider<RedisClusterConfiguration> clusterConfigurationProvider) {
63-
super(properties, sentinelConfigurationProvider, clusterConfigurationProvider);
65+
super(properties, standaloneConfigurationProvider, sentinelConfigurationProvider, clusterConfigurationProvider);
6466
}
6567

6668
@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
}

spring-boot-project/spring-boot-docs/src/docs/asciidoc/data/nosql.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ include::{docs-java}/data/nosql/redis/connecting/MyBean.java[]
4444
TIP: You can also register an arbitrary number of beans that implement `LettuceClientConfigurationBuilderCustomizer` for more advanced customizations.
4545
`ClientResources` can also be customized using `ClientResourcesBuilderCustomizer`.
4646
If you use Jedis, `JedisClientConfigurationBuilderCustomizer` is also available.
47+
Alternatively, you can register a bean of type `RedisStandaloneConfiguration`, `RedisSentinelConfiguration`, or `RedisClusterConfiguration` to take full control over the configuration.
4748

4849
If you add your own `@Bean` of any of the auto-configured types, it replaces the default (except in the case of `RedisTemplate`, when the exclusion is based on the bean name, `redisTemplate`, not its type).
4950

0 commit comments

Comments
 (0)