Skip to content

Commit 6819f48

Browse files
committed
DataSource 'spring.datasource.url' property should be considered if it was explicitly set
see gh-19192
1 parent c584334 commit 6819f48

File tree

5 files changed

+74
-10
lines changed

5 files changed

+74
-10
lines changed

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 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.
@@ -37,6 +37,7 @@
3737
import org.springframework.context.annotation.Configuration;
3838
import org.springframework.context.annotation.Import;
3939
import org.springframework.core.type.AnnotatedTypeMetadata;
40+
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
4041
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
4142

4243
/**
@@ -73,8 +74,9 @@ protected static class PooledDataSourceConfiguration {
7374
}
7475

7576
/**
76-
* {@link AnyNestedCondition} that checks that either {@code spring.datasource.type}
77-
* is set or {@link PooledDataSourceAvailableCondition} applies.
77+
* {@link AnyNestedCondition} that checks that either one of
78+
* {@code spring.datasource.type}, {@code spring.datasource.url} properties is set or
79+
* {@link PooledDataSourceAvailableCondition} applies.
7880
*/
7981
static class PooledDataSourceCondition extends AnyNestedCondition {
8082

@@ -87,6 +89,12 @@ static class ExplicitType {
8789

8890
}
8991

92+
@ConditionalOnProperty(prefix = "spring.datasource", name = "url")
93+
@ConditionalOnClass(SimpleDriverDataSource.class)
94+
static class ExplicitUrl {
95+
96+
}
97+
9098
@Conditional(PooledDataSourceAvailableCondition.class)
9199
static class PooledDataSourceAvailable {
92100

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

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 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.
@@ -20,13 +20,17 @@
2020

2121
import com.zaxxer.hikari.HikariDataSource;
2222

23+
import org.springframework.boot.autoconfigure.condition.AnyNestedCondition;
2324
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2425
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2526
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
2627
import org.springframework.boot.context.properties.ConfigurationProperties;
28+
import org.springframework.boot.jdbc.DataSourceBuilder;
2729
import org.springframework.boot.jdbc.DatabaseDriver;
2830
import org.springframework.context.annotation.Bean;
31+
import org.springframework.context.annotation.Conditional;
2932
import org.springframework.context.annotation.Configuration;
33+
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
3034
import org.springframework.util.StringUtils;
3135

3236
/**
@@ -114,12 +118,39 @@ org.apache.commons.dbcp2.BasicDataSource dataSource(DataSourceProperties propert
114118
*/
115119
@Configuration(proxyBeanMethods = false)
116120
@ConditionalOnMissingBean(DataSource.class)
117-
@ConditionalOnProperty(name = "spring.datasource.type")
121+
@Conditional(GenericCondition.class)
118122
static class Generic {
119123

120124
@Bean
121125
DataSource dataSource(DataSourceProperties properties) {
122-
return properties.initializeDataSourceBuilder().build();
126+
return properties.initializeDataSourceBuilder().type(determineType(properties)).build();
127+
}
128+
129+
private static Class<? extends DataSource> determineType(DataSourceProperties properties) {
130+
Class<? extends DataSource> type = properties.getType();
131+
if (type == null) {
132+
type = DataSourceBuilder.findType(properties.getClassLoader());
133+
}
134+
return (type != null) ? type : SimpleDriverDataSource.class;
135+
}
136+
137+
}
138+
139+
static class GenericCondition extends AnyNestedCondition {
140+
141+
GenericCondition() {
142+
super(ConfigurationPhase.PARSE_CONFIGURATION);
143+
}
144+
145+
@ConditionalOnProperty(prefix = "spring.datasource", name = "type")
146+
static class ExplicitType {
147+
148+
}
149+
150+
@ConditionalOnProperty(prefix = "spring.datasource", name = "url")
151+
@ConditionalOnClass(SimpleDriverDataSource.class)
152+
static class ExplicitUrl {
153+
123154
}
124155

125156
}

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 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.
@@ -158,6 +158,20 @@ void explicitTypeNoSupportedDataSource() {
158158
.run(this::containsOnlySimpleDriverDataSource);
159159
}
160160

161+
/**
162+
* This test makes sure that if no supported pool data source is present, a datasource
163+
* is still created if "spring.datasource.url" is present.
164+
*/
165+
@Test
166+
void explicitUrlSupportedDataSource() {
167+
this.contextRunner
168+
.withClassLoader(new FilteredClassLoader("org.apache.tomcat", "com.zaxxer.hikari",
169+
"org.apache.commons.dbcp", "org.apache.commons.dbcp2"))
170+
.withPropertyValues("spring.datasource.driverClassName:org.hsqldb.jdbcDriver",
171+
"spring.datasource.url:jdbc:hsqldb:mem:testdb")
172+
.run(this::containsOnlySimpleDriverDataSource);
173+
}
174+
161175
@Test
162176
void explicitTypeSupportedDataSource() {
163177
this.contextRunner
@@ -169,7 +183,8 @@ void explicitTypeSupportedDataSource() {
169183

170184
private void containsOnlySimpleDriverDataSource(AssertableApplicationContext context) {
171185
assertThat(context).hasSingleBean(DataSource.class);
172-
assertThat(context).getBean(DataSource.class).isExactlyInstanceOf(SimpleDriverDataSource.class);
186+
assertThat(context).getBean(DataSource.class).isExactlyInstanceOf(SimpleDriverDataSource.class)
187+
.extracting("driver").isNotNull();
173188
}
174189

175190
@Test

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 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.
@@ -89,6 +89,7 @@ private void bind(DataSource result) {
8989
ConfigurationPropertyNameAliases aliases = new ConfigurationPropertyNameAliases();
9090
aliases.addAliases("url", "jdbc-url");
9191
aliases.addAliases("username", "user");
92+
aliases.addAliases("driver-class-name", "driver-class");
9293
Binder binder = new Binder(source.withAliases(aliases));
9394
binder.bind(ConfigurationPropertyName.EMPTY, Bindable.ofInstance(result));
9495
}

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 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.
@@ -29,6 +29,8 @@
2929
import org.junit.jupiter.api.AfterEach;
3030
import org.junit.jupiter.api.Test;
3131

32+
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
33+
3234
import static org.assertj.core.api.Assertions.assertThat;
3335

3436
/**
@@ -68,6 +70,13 @@ void defaultToCommonsDbcp2AsLastResort() {
6870
assertThat(this.dataSource).isInstanceOf(BasicDataSource.class);
6971
}
7072

73+
@Test
74+
void simpleDriverDataSource() {
75+
this.dataSource = DataSourceBuilder.create().url("jdbc:h2:test").type(SimpleDriverDataSource.class).build();
76+
assertThat(this.dataSource).isInstanceOf(SimpleDriverDataSource.class);
77+
assertThat(this.dataSource).extracting("driver").isNotNull();
78+
}
79+
7180
@Test
7281
void specificTypeOfDataSource() {
7382
HikariDataSource hikariDataSource = DataSourceBuilder.create().type(HikariDataSource.class).build();

0 commit comments

Comments
 (0)