Skip to content

Commit dc56608

Browse files
committed
Merge branch '2.2.x'
Closes gh-21004
2 parents 619fe38 + 1a8aa72 commit dc56608

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@
3636
import org.springframework.context.annotation.Conditional;
3737
import org.springframework.context.annotation.Configuration;
3838
import org.springframework.context.annotation.Import;
39+
import org.springframework.core.env.Environment;
3940
import org.springframework.core.type.AnnotatedTypeMetadata;
4041
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
42+
import org.springframework.util.StringUtils;
4143

4244
/**
4345
* {@link EnableAutoConfiguration Auto-configuration} for {@link DataSource}.
@@ -125,8 +127,7 @@ static class EmbeddedDatabaseCondition extends SpringBootCondition {
125127
@Override
126128
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
127129
ConditionMessage.Builder message = ConditionMessage.forCondition("EmbeddedDataSource");
128-
boolean hasDatasourceUrl = context.getEnvironment().containsProperty(DATASOURCE_URL_PROPERTY);
129-
if (hasDatasourceUrl) {
130+
if (hasDataSourceUrlProperty(context)) {
130131
return ConditionOutcome.noMatch(message.because(DATASOURCE_URL_PROPERTY + " is set"));
131132
}
132133
if (anyMatches(context, metadata, this.pooledCondition)) {
@@ -139,6 +140,19 @@ public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeM
139140
return ConditionOutcome.match(message.found("embedded database").items(type));
140141
}
141142

143+
private boolean hasDataSourceUrlProperty(ConditionContext context) {
144+
Environment environment = context.getEnvironment();
145+
if (environment.containsProperty(DATASOURCE_URL_PROPERTY)) {
146+
try {
147+
return StringUtils.hasText(environment.getProperty(DATASOURCE_URL_PROPERTY));
148+
}
149+
catch (IllegalArgumentException ex) {
150+
// Ignore unresolvable placeholder errors
151+
}
152+
}
153+
return false;
154+
}
155+
142156
}
143157

144158
}

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
@@ -49,6 +49,7 @@
4949
import org.springframework.context.annotation.Configuration;
5050
import org.springframework.jdbc.core.JdbcTemplate;
5151
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
52+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase;
5253
import org.springframework.util.StringUtils;
5354

5455
import static org.assertj.core.api.Assertions.assertThat;
@@ -209,6 +210,12 @@ void whenThereIsAUserProvidedDataSourceAnUnresolvablePlaceholderDoesNotCauseAPro
209210
.run((context) -> assertThat(context).getBean(DataSource.class).isInstanceOf(BasicDataSource.class));
210211
}
211212

213+
@Test
214+
void whenThereIsAnEmptyUserProvidedDataSource() {
215+
this.contextRunner.with(hideConnectionPools()).withPropertyValues("spring.datasource.url:")
216+
.run((context) -> assertThat(context).getBean(DataSource.class).isInstanceOf(EmbeddedDatabase.class));
217+
}
218+
212219
@Test
213220
void testDataSourceIsInitializedEarly() {
214221
this.contextRunner.withUserConfiguration(TestInitializedDataSourceConfiguration.class)

0 commit comments

Comments
 (0)