Add pool parameters to AbstractDataSourceConfiguration

* Exposed common pool properties in the base class for data source configuration
* Made it @ConfigurationProperties so it binds in strongly typed sense

[Fixes #53028455] [bs-214] Add pool parameters to AbstractDataSourceConfiguration
pull/23/merge
Dave Syer 11 years ago
parent 56f5b3ad0f
commit 68e84f7d02

@ -17,7 +17,7 @@
package org.springframework.boot.autoconfigure.jdbc;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.util.StringUtils;
@ -26,19 +26,28 @@ import org.springframework.util.StringUtils;
*
* @author Dave Syer
*/
public class AbstractDataSourceConfiguration {
@ConfigurationProperties(path = "spring.data")
public abstract class AbstractDataSourceConfiguration {
@Value("${spring.database.driverClassName:}")
private String driverClassName;
@Value("${spring.database.url:}")
private String url;
@Value("${spring.database.username:sa}")
private String username;
private String username = "sa";
@Value("${spring.database.password:}")
private String password;
private String password = "";
private int maxActive = 8;
private int maxIdle = 8;
private int minIdle = 8;
private String validationQuery;
private boolean testOnBorrow = false;
private boolean testOnReturn = false;
protected String getDriverClassName() {
if (StringUtils.hasText(this.driverClassName)) {
@ -74,6 +83,46 @@ public class AbstractDataSourceConfiguration {
return this.url;
}
public void setDriverClassName(String driverClassName) {
this.driverClassName = driverClassName;
}
public void setUrl(String url) {
this.url = url;
}
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
public void setMaxActive(int maxActive) {
this.maxActive = maxActive;
}
public void setMaxIdle(int maxIdle) {
this.maxIdle = maxIdle;
}
public void setMinIdle(int minIdle) {
this.minIdle = minIdle;
}
public void setValidationQuery(String validationQuery) {
this.validationQuery = validationQuery;
}
public void setTestOnBorrow(boolean testOnBorrow) {
this.testOnBorrow = testOnBorrow;
}
public void setTestOnReturn(boolean testOnReturn) {
this.testOnReturn = testOnReturn;
}
protected String getUsername() {
return this.username;
}
@ -81,4 +130,29 @@ public class AbstractDataSourceConfiguration {
protected String getPassword() {
return this.password;
}
protected int getMaxActive() {
return this.maxActive;
}
protected int getMaxIdle() {
return this.maxIdle;
}
protected int getMinIdle() {
return this.minIdle;
}
protected String getValidationQuery() {
return this.validationQuery;
}
protected boolean isTestOnBorrow() {
return this.testOnBorrow;
}
protected boolean isTestOnReturn() {
return this.testOnReturn;
}
}

@ -50,6 +50,12 @@ public class BasicDataSourceConfiguration extends AbstractDataSourceConfiguratio
this.pool.setUrl(getUrl());
this.pool.setUsername(getUsername());
this.pool.setPassword(getPassword());
this.pool.setMaxActive(getMaxActive());
this.pool.setMaxIdle(getMaxIdle());
this.pool.setMinIdle(getMinIdle());
this.pool.setTestOnBorrow(isTestOnBorrow());
this.pool.setTestOnReturn(isTestOnBorrow());
this.pool.setValidationQuery(getValidationQuery());
return this.pool;
}

@ -40,6 +40,12 @@ public class TomcatDataSourceConfiguration extends AbstractDataSourceConfigurati
this.pool.setUrl(getUrl());
this.pool.setUsername(getUsername());
this.pool.setPassword(getPassword());
this.pool.setMaxActive(getMaxActive());
this.pool.setMaxIdle(getMaxIdle());
this.pool.setMinIdle(getMinIdle());
this.pool.setTestOnBorrow(isTestOnBorrow());
this.pool.setTestOnReturn(isTestOnBorrow());
this.pool.setValidationQuery(getValidationQuery());
return this.pool;
}

@ -49,7 +49,16 @@ public class EnableConfigurationPropertiesTests {
TestUtils.addEnviroment(this.context, "name:foo");
this.context.refresh();
assertEquals(1, this.context.getBeanNamesForType(TestProperties.class).length);
assertEquals("foo", this.context.getBean(TestProperties.class).getName());
assertEquals("foo", this.context.getBean(TestProperties.class).name);
}
@Test
public void testBasicPropertiesBindingWithAnnotationOnBaseClass() {
this.context.register(DerivedConfiguration.class);
TestUtils.addEnviroment(this.context, "name:foo");
this.context.refresh();
assertEquals(1, this.context.getBeanNamesForType(DerivedProperties.class).length);
assertEquals("foo", this.context.getBean(BaseProperties.class).name);
}
@Test
@ -75,7 +84,7 @@ public class EnableConfigurationPropertiesTests {
TestUtils.addEnviroment(this.context, "name:foo");
this.context.refresh();
assertEquals(1, this.context.getBeanNamesForType(MoreProperties.class).length);
assertEquals("foo", this.context.getBean(MoreProperties.class).getName());
assertEquals("foo", this.context.getBean(MoreProperties.class).name);
}
@Test
@ -84,7 +93,7 @@ public class EnableConfigurationPropertiesTests {
this.context.refresh();
String[] beanNames = this.context.getBeanNamesForType(TestProperties.class);
assertEquals("Wrong beans: " + Arrays.asList(beanNames), 1, beanNames.length);
assertEquals("bar", this.context.getBean(TestProperties.class).getName());
assertEquals("bar", this.context.getBean(TestProperties.class).name);
}
@Test
@ -93,7 +102,7 @@ public class EnableConfigurationPropertiesTests {
this.context.refresh();
String[] beanNames = this.context.getBeanNamesForType(TestProperties.class);
assertEquals("Wrong beans: " + Arrays.asList(beanNames), 1, beanNames.length);
assertEquals("bar", this.context.getBean(TestProperties.class).getName());
assertEquals("bar", this.context.getBean(TestProperties.class).name);
}
@Test
@ -102,8 +111,7 @@ public class EnableConfigurationPropertiesTests {
this.context.refresh();
assertEquals(1,
this.context.getBeanNamesForType(ResourceBindingProperties.class).length);
assertEquals("foo", this.context.getBean(ResourceBindingProperties.class)
.getName());
assertEquals("foo", this.context.getBean(ResourceBindingProperties.class).name);
}
@Test
@ -113,8 +121,7 @@ public class EnableConfigurationPropertiesTests {
this.context.refresh();
assertEquals(1,
this.context.getBeanNamesForType(ResourceBindingProperties.class).length);
assertEquals("other", this.context.getBean(ResourceBindingProperties.class)
.getName());
assertEquals("other", this.context.getBean(ResourceBindingProperties.class).name);
}
@Test
@ -124,8 +131,7 @@ public class EnableConfigurationPropertiesTests {
this.context.refresh();
assertEquals(1,
this.context.getBeanNamesForType(ResourceBindingProperties.class).length);
assertEquals("foo", this.context.getBean(ResourceBindingProperties.class)
.getName());
assertEquals("foo", this.context.getBean(ResourceBindingProperties.class).name);
}
@Test
@ -135,8 +141,7 @@ public class EnableConfigurationPropertiesTests {
this.context.refresh();
assertEquals(1,
this.context.getBeanNamesForType(ResourceBindingProperties.class).length);
assertEquals("bar", this.context.getBean(ResourceBindingProperties.class)
.getName());
assertEquals("bar", this.context.getBean(ResourceBindingProperties.class).name);
}
@Test
@ -180,6 +185,11 @@ public class EnableConfigurationPropertiesTests {
protected static class TestConfiguration {
}
@Configuration
@EnableConfigurationProperties(DerivedProperties.class)
protected static class DerivedConfiguration {
}
@Configuration
protected static class DefaultConfiguration {
@Bean
@ -206,7 +216,7 @@ public class EnableConfigurationPropertiesTests {
}
public String getName() {
return this.properties.getName();
return this.properties.name;
}
}
@ -215,15 +225,25 @@ public class EnableConfigurationPropertiesTests {
protected static class MoreConfiguration {
}
@ConfigurationProperties
protected static class BaseProperties {
private String name;
public void setName(String name) {
this.name = name;
}
}
protected static class DerivedProperties extends BaseProperties {
}
@ConfigurationProperties
protected static class TestProperties {
private String name;
private int[] array;
private List<Integer> list = new ArrayList<Integer>();
public String getName() {
return this.name;
}
// No getter - you should be able to bind to a write-only bean
public void setName(String name) {
this.name = name;
@ -245,25 +265,21 @@ public class EnableConfigurationPropertiesTests {
protected static class MoreProperties {
private String name;
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
// No getter - you should be able to bind to a write-only bean
}
@ConfigurationProperties(path = "${binding.location:classpath:name.yml}")
protected static class ResourceBindingProperties {
private String name;
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
// No getter - you should be able to bind to a write-only bean
}
}

Loading…
Cancel
Save