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; package org.springframework.boot.autoconfigure.jdbc;
import org.springframework.beans.factory.BeanCreationException; 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.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
@ -26,19 +26,28 @@ import org.springframework.util.StringUtils;
* *
* @author Dave Syer * @author Dave Syer
*/ */
public class AbstractDataSourceConfiguration { @ConfigurationProperties(path = "spring.data")
public abstract class AbstractDataSourceConfiguration {
@Value("${spring.database.driverClassName:}")
private String driverClassName; private String driverClassName;
@Value("${spring.database.url:}")
private String url; private String url;
@Value("${spring.database.username:sa}") private String username = "sa";
private String username;
@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() { protected String getDriverClassName() {
if (StringUtils.hasText(this.driverClassName)) { if (StringUtils.hasText(this.driverClassName)) {
@ -74,6 +83,46 @@ public class AbstractDataSourceConfiguration {
return this.url; 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() { protected String getUsername() {
return this.username; return this.username;
} }
@ -81,4 +130,29 @@ public class AbstractDataSourceConfiguration {
protected String getPassword() { protected String getPassword() {
return this.password; 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.setUrl(getUrl());
this.pool.setUsername(getUsername()); this.pool.setUsername(getUsername());
this.pool.setPassword(getPassword()); 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; return this.pool;
} }

@ -40,6 +40,12 @@ public class TomcatDataSourceConfiguration extends AbstractDataSourceConfigurati
this.pool.setUrl(getUrl()); this.pool.setUrl(getUrl());
this.pool.setUsername(getUsername()); this.pool.setUsername(getUsername());
this.pool.setPassword(getPassword()); 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; return this.pool;
} }

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

Loading…
Cancel
Save