Skip to content

Commit 1a8aa72

Browse files
committed
Merge branch '2.1.x' into 2.2.x
Closes gh-21003
2 parents aebf475 + 822d9f6 commit 1a8aa72

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}.
@@ -124,8 +126,7 @@ static class EmbeddedDatabaseCondition extends SpringBootCondition {
124126
@Override
125127
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
126128
ConditionMessage.Builder message = ConditionMessage.forCondition("EmbeddedDataSource");
127-
boolean hasDatasourceUrl = context.getEnvironment().containsProperty(DATASOURCE_URL_PROPERTY);
128-
if (hasDatasourceUrl) {
129+
if (hasDataSourceUrlProperty(context)) {
129130
return ConditionOutcome.noMatch(message.because(DATASOURCE_URL_PROPERTY + " is set"));
130131
}
131132
if (anyMatches(context, metadata, this.pooledCondition)) {
@@ -138,6 +139,19 @@ public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeM
138139
return ConditionOutcome.match(message.found("embedded database").items(type));
139140
}
140141

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

143157
}

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

5354
import static org.assertj.core.api.Assertions.assertThat;
@@ -201,6 +202,12 @@ void whenThereIsAUserProvidedDataSourceAnUnresolvablePlaceholderDoesNotCauseAPro
201202
.run((context) -> assertThat(context).getBean(DataSource.class).isInstanceOf(BasicDataSource.class));
202203
}
203204

205+
@Test
206+
void whenThereIsAnEmptyUserProvidedDataSource() {
207+
this.contextRunner.with(hideConnectionPools()).withPropertyValues("spring.datasource.url:")
208+
.run((context) -> assertThat(context).getBean(DataSource.class).isInstanceOf(EmbeddedDatabase.class));
209+
}
210+
204211
@Test
205212
void testDataSourceIsInitializedEarly() {
206213
this.contextRunner.withUserConfiguration(TestInitializedDataSourceConfiguration.class)

0 commit comments

Comments
 (0)