Skip to content

Commit 42494b5

Browse files
committed
Parse redis database from url if present
Fix GH-43810 Signed-off-by: Yanming Zhou <zhouyanming@gmail.com>
1 parent e2a62d6 commit 42494b5

File tree

4 files changed

+27
-15
lines changed

4 files changed

+27
-15
lines changed

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

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 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.
@@ -27,6 +27,7 @@
2727
* @author Andy Wilkinson
2828
* @author Phillip Webb
2929
* @author Scott Frederick
30+
* @author Yanming Zhou
3031
*/
3132
class PropertiesRedisConnectionDetails implements RedisConnectionDetails {
3233

@@ -39,7 +40,7 @@ class PropertiesRedisConnectionDetails implements RedisConnectionDetails {
3940
@Override
4041
public String getUsername() {
4142
if (this.properties.getUrl() != null) {
42-
ConnectionInfo connectionInfo = connectionInfo(this.properties.getUrl());
43+
ConnectionInfo connectionInfo = RedisConnectionConfiguration.parseUrl(this.properties.getUrl());
4344
return connectionInfo.getUsername();
4445
}
4546
return this.properties.getUsername();
@@ -48,7 +49,7 @@ public String getUsername() {
4849
@Override
4950
public String getPassword() {
5051
if (this.properties.getUrl() != null) {
51-
ConnectionInfo connectionInfo = connectionInfo(this.properties.getUrl());
52+
ConnectionInfo connectionInfo = RedisConnectionConfiguration.parseUrl(this.properties.getUrl());
5253
return connectionInfo.getPassword();
5354
}
5455
return this.properties.getPassword();
@@ -57,17 +58,14 @@ public String getPassword() {
5758
@Override
5859
public Standalone getStandalone() {
5960
if (this.properties.getUrl() != null) {
60-
ConnectionInfo connectionInfo = connectionInfo(this.properties.getUrl());
61+
ConnectionInfo connectionInfo = RedisConnectionConfiguration.parseUrl(this.properties.getUrl());
6162
return Standalone.of(connectionInfo.getUri().getHost(), connectionInfo.getUri().getPort(),
62-
this.properties.getDatabase());
63+
(connectionInfo.getDatabase() != null) ? connectionInfo.getDatabase()
64+
: this.properties.getDatabase());
6365
}
6466
return Standalone.of(this.properties.getHost(), this.properties.getPort(), this.properties.getDatabase());
6567
}
6668

67-
private ConnectionInfo connectionInfo(String url) {
68-
return (url != null) ? RedisConnectionConfiguration.parseUrl(url) : null;
69-
}
70-
7169
@Override
7270
public Sentinel getSentinel() {
7371
org.springframework.boot.autoconfigure.data.redis.RedisProperties.Sentinel sentinel = this.properties

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

+16-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 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.
@@ -45,6 +45,7 @@
4545
* @author Moritz Halbritter
4646
* @author Andy Wilkinson
4747
* @author Phillip Webb
48+
* @author Yanming Zhou
4849
*/
4950
abstract class RedisConnectionConfiguration {
5051

@@ -200,7 +201,12 @@ static ConnectionInfo parseUrl(String url) {
200201
password = candidate;
201202
}
202203
}
203-
return new ConnectionInfo(uri, useSsl, username, password);
204+
Integer database = null;
205+
String[] pathSplit = uri.getPath().split("/", 2);
206+
if (pathSplit.length > 1 && !pathSplit[1].isEmpty()) {
207+
database = Integer.parseInt(pathSplit[1]);
208+
}
209+
return new ConnectionInfo(uri, useSsl, username, password, database);
204210
}
205211
catch (URISyntaxException ex) {
206212
throw new RedisUrlSyntaxException(url, ex);
@@ -217,11 +223,14 @@ static class ConnectionInfo {
217223

218224
private final String password;
219225

220-
ConnectionInfo(URI uri, boolean useSsl, String username, String password) {
226+
private final Integer database;
227+
228+
ConnectionInfo(URI uri, boolean useSsl, String username, String password, Integer database) {
221229
this.uri = uri;
222230
this.useSsl = useSsl;
223231
this.username = username;
224232
this.password = password;
233+
this.database = database;
225234
}
226235

227236
URI getUri() {
@@ -240,6 +249,10 @@ String getPassword() {
240249
return this.password;
241250
}
242251

252+
Integer getDatabase() {
253+
return this.database;
254+
}
255+
243256
}
244257

245258
}

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
* @author Mark Paluch
3232
* @author Stephane Nicoll
3333
* @author Scott Frederick
34+
* @author Yanming Zhou
3435
* @since 1.0.0
3536
*/
3637
@ConfigurationProperties(prefix = "spring.data.redis")
@@ -42,8 +43,8 @@ public class RedisProperties {
4243
private int database = 0;
4344

4445
/**
45-
* Connection URL. Overrides host, port, username, and password. Example:
46-
* redis://user:password@example.com:6379
46+
* Connection URL. Overrides host, port, username, password, and database. Example:
47+
* redis://user:password@example.com:6379/8
4748
*/
4849
private String url;
4950

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ void standaloneIsConfiguredFromUrl() {
103103
RedisConnectionDetails.Standalone standalone = connectionDetails.getStandalone();
104104
assertThat(standalone.getHost()).isEqualTo("example.com");
105105
assertThat(standalone.getPort()).isEqualTo(1234);
106-
assertThat(standalone.getDatabase()).isEqualTo(5);
106+
assertThat(standalone.getDatabase()).isEqualTo(9999);
107107
}
108108

109109
@Test

0 commit comments

Comments
 (0)