Skip to content

Commit 73a08dd

Browse files
committed
Avoid overriding beans and ensure import order is used for DataSource
During processing of a configuration class, the class's complete hierarchy is processed and during the processing of each class its member classes are processed. Previously, each pool-specific inner-class of DataSourceConfiguration extended the abstract outer class. This meant that when the import from DataSourceAutoConfiguration.PooledDataSourceConfiguration caused the first pool-specific inner-class to be processed, DataSourceConfiguration would be processed as it was the inner-class's superclass. In turn all of DataSourceConfiguration's member classes would then be processed. This caused the first import (of DataSourceConfiguration.Tomcat) to trigger processing of all of the other pool-specific inner-classes in whatever order they were found rather than them being processed in the order in which they are imported by DataSourceAutoConfiguration.PooledDataSourceConfiguration. Another part of the problem was that none of the pool-specific inner-classes were conditional on a missing DataSource bean. This meant that, when multiple pools were on the classpath, each class after the first would override the previous class's definition of the DataSource bean. This commit updates each of the pool-specific inner-classes so that they no longer extend DataSourceConfiguration. This ensures that the inner classes are processed in the order defined in the import on PooledDataSourceConfiguration. Each of the classes has also been annotated with @ConditionalOnMissingBean(DataSource.class). This prevents the DataSource bean definition from being overridden and ensures that the order of precedence for the pool that will be used is as defined in the import. Closes gh-13737
1 parent 8b2cb32 commit 73a08dd

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 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,7 +37,7 @@
3737
abstract class DataSourceConfiguration {
3838

3939
@SuppressWarnings("unchecked")
40-
protected <T> T createDataSource(DataSourceProperties properties,
40+
protected static <T> T createDataSource(DataSourceProperties properties,
4141
Class<? extends DataSource> type) {
4242
return (T) properties.initializeDataSourceBuilder().type(type).build();
4343
}
@@ -46,8 +46,9 @@ protected <T> T createDataSource(DataSourceProperties properties,
4646
* Tomcat Pool DataSource configuration.
4747
*/
4848
@ConditionalOnClass(org.apache.tomcat.jdbc.pool.DataSource.class)
49+
@ConditionalOnMissingBean(DataSource.class)
4950
@ConditionalOnProperty(name = "spring.datasource.type", havingValue = "org.apache.tomcat.jdbc.pool.DataSource", matchIfMissing = true)
50-
static class Tomcat extends DataSourceConfiguration {
51+
static class Tomcat {
5152

5253
@Bean
5354
@ConfigurationProperties(prefix = "spring.datasource.tomcat")
@@ -71,8 +72,9 @@ public org.apache.tomcat.jdbc.pool.DataSource dataSource(
7172
* Hikari DataSource configuration.
7273
*/
7374
@ConditionalOnClass(HikariDataSource.class)
75+
@ConditionalOnMissingBean(DataSource.class)
7476
@ConditionalOnProperty(name = "spring.datasource.type", havingValue = "com.zaxxer.hikari.HikariDataSource", matchIfMissing = true)
75-
static class Hikari extends DataSourceConfiguration {
77+
static class Hikari {
7678

7779
@Bean
7880
@ConfigurationProperties(prefix = "spring.datasource.hikari")
@@ -88,9 +90,10 @@ public HikariDataSource dataSource(DataSourceProperties properties) {
8890
* @deprecated as of 1.5 in favor of DBCP2
8991
*/
9092
@ConditionalOnClass(org.apache.commons.dbcp.BasicDataSource.class)
93+
@ConditionalOnMissingBean(DataSource.class)
9194
@ConditionalOnProperty(name = "spring.datasource.type", havingValue = "org.apache.commons.dbcp.BasicDataSource", matchIfMissing = true)
9295
@Deprecated
93-
static class Dbcp extends DataSourceConfiguration {
96+
static class Dbcp {
9497

9598
@Bean
9699
@ConfigurationProperties(prefix = "spring.datasource.dbcp")
@@ -114,8 +117,9 @@ public org.apache.commons.dbcp.BasicDataSource dataSource(
114117
* DBCP DataSource configuration.
115118
*/
116119
@ConditionalOnClass(org.apache.commons.dbcp2.BasicDataSource.class)
120+
@ConditionalOnMissingBean(DataSource.class)
117121
@ConditionalOnProperty(name = "spring.datasource.type", havingValue = "org.apache.commons.dbcp2.BasicDataSource", matchIfMissing = true)
118-
static class Dbcp2 extends DataSourceConfiguration {
122+
static class Dbcp2 {
119123

120124
@Bean
121125
@ConfigurationProperties(prefix = "spring.datasource.dbcp2")

0 commit comments

Comments
 (0)