Skip to content

Commit 1835323

Browse files
committed
Do not fail if "spring.datasource.url" cannot be resolved
Previously, a condition checked the value of "spring.datasource.url" to determine if an embedded database has to be created as a fallback. When the value is set with an unresolved placeholder, this fails even if the DataSource is going to created by another mean ultimately. This commit makes a more conservative check by only checking the presence of the property rather than its value. Closes gh-20438
1 parent 9a5ffb7 commit 1835323

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfiguration.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import org.springframework.context.annotation.Import;
3939
import org.springframework.core.type.AnnotatedTypeMetadata;
4040
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
41-
import org.springframework.util.StringUtils;
4241

4342
/**
4443
* {@link EnableAutoConfiguration Auto-configuration} for {@link DataSource}.
@@ -129,14 +128,16 @@ private ClassLoader getDataSourceClassLoader(ConditionContext context) {
129128
*/
130129
static class EmbeddedDatabaseCondition extends SpringBootCondition {
131130

131+
private static final String DATASOURCE_URL_PROPERTY = "spring.datasource.url";
132+
132133
private final SpringBootCondition pooledCondition = new PooledDataSourceCondition();
133134

134135
@Override
135136
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
136137
ConditionMessage.Builder message = ConditionMessage.forCondition("EmbeddedDataSource");
137-
String url = context.getEnvironment().getProperty("spring.datasource.url");
138-
if (StringUtils.hasText(url)) {
139-
return ConditionOutcome.noMatch(message.found("explicit url").items(url));
138+
boolean hasDatasourceUrl = context.getEnvironment().containsProperty(DATASOURCE_URL_PROPERTY);
139+
if (hasDatasourceUrl) {
140+
return ConditionOutcome.noMatch(message.because(DATASOURCE_URL_PROPERTY + " is set"));
140141
}
141142
if (anyMatches(context, metadata, this.pooledCondition)) {
142143
return ConditionOutcome.noMatch(message.foundExactly("supported pooled data source"));

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfigurationTests.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,13 @@ public void testDefaultDataSourceCanBeOverridden() {
194194
.run((context) -> assertThat(context).getBean(DataSource.class).isInstanceOf(BasicDataSource.class));
195195
}
196196

197+
@Test
198+
public void whenThereIsAUserProvidedDataSourceAnUnresolvablePlaceholderDoesNotCauseAProblem() {
199+
this.contextRunner.withUserConfiguration(TestDataSourceConfiguration.class)
200+
.withPropertyValues("spring.datasource.url:${UNRESOLVABLE_PLACEHOLDER}")
201+
.run((context) -> assertThat(context).getBean(DataSource.class).isInstanceOf(BasicDataSource.class));
202+
}
203+
197204
@Test
198205
public void testDataSourceIsInitializedEarly() {
199206
this.contextRunner.withUserConfiguration(TestInitializedDataSourceConfiguration.class)

0 commit comments

Comments
 (0)