diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceProperties.java index 1132a79b0e6..479fee05fec 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceProperties.java @@ -42,6 +42,11 @@ public class DataSourceProperties implements BeanClassLoaderAware, InitializingB public static final String PREFIX = "spring.datasource"; + /** + * Name of the datasource. + */ + private String name = "testdb"; + /** * Fully qualified name of the connection pool implementation to use. By default, * it is auto-detected from the classpath. @@ -126,6 +131,14 @@ public class DataSourceProperties implements BeanClassLoaderAware, InitializingB .get(this.classLoader); } + public String getName() { + return this.name; + } + + public void setName(String name) { + this.name = name; + } + public Class getType() { return type; } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/EmbeddedDataSourceConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/EmbeddedDataSourceConfiguration.java index 55da0e19d64..ac21ed129f8 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/EmbeddedDataSourceConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/EmbeddedDataSourceConfiguration.java @@ -19,7 +19,8 @@ package org.springframework.boot.autoconfigure.jdbc; import javax.annotation.PreDestroy; import org.springframework.beans.factory.BeanClassLoaderAware; -import org.springframework.beans.factory.annotation.Value; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabase; @@ -29,17 +30,19 @@ import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; * Configuration for embedded data sources. * * @author Phillip Webb + * @author Stephane Nicoll * @see DataSourceAutoConfiguration */ @Configuration +@EnableConfigurationProperties(DataSourceProperties.class) public class EmbeddedDataSourceConfiguration implements BeanClassLoaderAware { private EmbeddedDatabase database; private ClassLoader classLoader; - @Value("${spring.datasource.name:testdb}") - private String name = "testdb"; + @Autowired + private DataSourceProperties properties; @Override public void setBeanClassLoader(ClassLoader classLoader) { @@ -50,7 +53,7 @@ public class EmbeddedDataSourceConfiguration implements BeanClassLoaderAware { public EmbeddedDatabase dataSource() { EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder() .setType(EmbeddedDatabaseConnection.get(this.classLoader).getType()); - this.database = builder.setName(this.name).build(); + this.database = builder.setName(this.properties.getName()).build(); return this.database; } diff --git a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc index 569cb641d87..33b2281787e 100644 --- a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -310,7 +310,7 @@ content into your application; rather pick only the properties that you need. security.oauth2.sso.login-path= # Path to the login page, i.e. the one that triggers the redirect to the OAuth2 Authorization Server # DATASOURCE ({sc-spring-boot-autoconfigure}/jdbc/DataSourceAutoConfiguration.{sc-ext}[DataSourceAutoConfiguration] & {sc-spring-boot-autoconfigure}/jdbc/DataSourceProperties.{sc-ext}[DataSourceProperties]) - spring.datasource.name= # name of the data source + spring.datasource.name=testdb # name of the data source spring.datasource.initialize=true # populate using data.sql spring.datasource.schema= # a schema (DDL) script resource reference spring.datasource.data= # a data (DML) script resource reference