Skip to content

Commit 7255124

Browse files
committed
Allow XADataSource auto-config with no spring.datasource properties
Fixes gh-20229
1 parent 5ea8bb0 commit 7255124

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

spring-boot-project/spring-boot-autoconfigure/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,11 @@
877877
<artifactId>transactions-jms</artifactId>
878878
<scope>test</scope>
879879
</dependency>
880+
<dependency>
881+
<groupId>com.ibm.db2</groupId>
882+
<artifactId>jcc</artifactId>
883+
<scope>test</scope>
884+
</dependency>
880885
<dependency>
881886
<groupId>com.jayway.jsonpath</groupId>
882887
<artifactId>json-path</artifactId>

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

Lines changed: 16 additions & 7 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.
@@ -13,9 +13,11 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
1716
package org.springframework.boot.autoconfigure.jdbc;
1817

18+
import java.util.HashMap;
19+
import java.util.Map;
20+
1921
import javax.sql.DataSource;
2022
import javax.sql.XADataSource;
2123
import javax.transaction.TransactionManager;
@@ -28,6 +30,7 @@
2830
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
2931
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
3032
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
33+
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.DataSourceBeanCreationException;
3134
import org.springframework.boot.context.properties.EnableConfigurationProperties;
3235
import org.springframework.boot.context.properties.bind.Bindable;
3336
import org.springframework.boot.context.properties.bind.Binder;
@@ -102,11 +105,17 @@ private void bindXaProperties(XADataSource target, DataSourceProperties dataSour
102105
}
103106

104107
private ConfigurationPropertySource getBinderSource(DataSourceProperties dataSourceProperties) {
105-
MapConfigurationPropertySource source = new MapConfigurationPropertySource();
106-
source.put("user", dataSourceProperties.determineUsername());
107-
source.put("password", dataSourceProperties.determinePassword());
108-
source.put("url", dataSourceProperties.determineUrl());
109-
source.putAll(dataSourceProperties.getXa().getProperties());
108+
Map<Object, Object> properties = new HashMap<Object, Object>();
109+
properties.putAll(dataSourceProperties.getXa().getProperties());
110+
properties.computeIfAbsent("user", (key) -> dataSourceProperties.determineUsername());
111+
properties.computeIfAbsent("password", (key) -> dataSourceProperties.determinePassword());
112+
try {
113+
properties.computeIfAbsent("url", (key) -> dataSourceProperties.determineUrl());
114+
}
115+
catch (DataSourceBeanCreationException ex) {
116+
// Continue as not all XA DataSource's require a URL
117+
}
118+
MapConfigurationPropertySource source = new MapConfigurationPropertySource(properties);
110119
ConfigurationPropertyNameAliases aliases = new ConfigurationPropertyNameAliases();
111120
aliases.addAliases("user", "username");
112121
return source.withAliases(aliases);

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

Lines changed: 19 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.
@@ -19,10 +19,14 @@
1919
import javax.sql.DataSource;
2020
import javax.sql.XADataSource;
2121

22+
import com.ibm.db2.jcc.DB2XADataSource;
2223
import org.hsqldb.jdbc.pool.JDBCXADataSource;
2324
import org.junit.jupiter.api.Test;
2425

26+
import org.springframework.boot.autoconfigure.AutoConfigurations;
2527
import org.springframework.boot.jdbc.XADataSourceWrapper;
28+
import org.springframework.boot.test.context.FilteredClassLoader;
29+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
2630
import org.springframework.boot.test.util.TestPropertyValues;
2731
import org.springframework.context.ApplicationContext;
2832
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@@ -60,6 +64,20 @@ void createFromUrl() {
6064
assertThat(dataSource.getUser()).isEqualTo("un");
6165
}
6266

67+
@Test
68+
void createNonEmbeddedFromXAProperties() {
69+
new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(XADataSourceAutoConfiguration.class))
70+
.withUserConfiguration(FromProperties.class)
71+
.withClassLoader(new FilteredClassLoader("org.h2.Driver", "org.hsqldb.jdbcDriver"))
72+
.withPropertyValues("spring.datasource.xa.data-source-class-name:com.ibm.db2.jcc.DB2XADataSource",
73+
"spring.datasource.xa.properties.user:test", "spring.datasource.xa.properties.password:secret")
74+
.run((context) -> {
75+
MockXADataSourceWrapper wrapper = context.getBean(MockXADataSourceWrapper.class);
76+
XADataSource xaDataSource = wrapper.getXaDataSource();
77+
assertThat(xaDataSource).isInstanceOf(DB2XADataSource.class);
78+
});
79+
}
80+
6381
@Test
6482
void createFromClass() throws Exception {
6583
ApplicationContext context = createContext(FromProperties.class,

0 commit comments

Comments
 (0)