From e6766f81a32b1a6d59383662c63863863ef66ba4 Mon Sep 17 00:00:00 2001 From: lihengming <89921218@qq.com> Date: Mon, 24 Jul 2017 10:27:52 +0800 Subject: [PATCH 1/2] update > optimize configuration-metadata for help IDE hint --- .../additional-spring-configuration-metadata.json | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/druid-spring-boot-starter/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/druid-spring-boot-starter/src/main/resources/META-INF/additional-spring-configuration-metadata.json index 03e5404c88..c4ebded281 100644 --- a/druid-spring-boot-starter/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/druid-spring-boot-starter/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -2,57 +2,67 @@ "properties": [ { "name": "spring.datasource.druid.connection-properties", - "type": "java.lang.String" + "type": "java.lang.String", + "sourceType": "com.alibaba.druid.pool.DruidDataSource" }, { "name": "spring.datasource.druid.filters", - "type": "java.lang.String" + "type": "java.lang.String", + "sourceType": "com.alibaba.druid.pool.DruidDataSource" }, { "name": "spring.datasource.druid.filter.stat.enabled", "type": "java.lang.Boolean", + "sourceType": "com.alibaba.druid.spring.boot.autoconfigure.stat.DruidFilterConfiguration", "description": "Enable StatFilter.", "defaultValue": true }, { "name": "spring.datasource.druid.filter.config.enabled", "type": "java.lang.Boolean", + "sourceType": "com.alibaba.druid.spring.boot.autoconfigure.stat.DruidFilterConfiguration", "description": "Enable ConfigFilter.", "defaultValue": false }, { "name": "spring.datasource.druid.filter.encoding.enabled", "type": "java.lang.Boolean", + "sourceType": "com.alibaba.druid.spring.boot.autoconfigure.stat.DruidFilterConfiguration", "description": "Enable EncodingConvertFilter.", "defaultValue": false }, { "name": "spring.datasource.druid.filter.slf4j.enabled", "type": "java.lang.Boolean", + "sourceType": "com.alibaba.druid.spring.boot.autoconfigure.stat.DruidFilterConfiguration", "description": "Enable Slf4jLogFilter.", "defaultValue": false }, { "name": "spring.datasource.druid.filter.log4j.enabled", "type": "java.lang.Boolean", + "sourceType": "com.alibaba.druid.spring.boot.autoconfigure.stat.DruidFilterConfiguration", "description": "Enable Log4jFilter.", "defaultValue": false }, { "name": "spring.datasource.druid.filter.log4j2.enabled", "type": "java.lang.Boolean", + "sourceType": "com.alibaba.druid.spring.boot.autoconfigure.stat.DruidFilterConfiguration", "description": "Enable Log4j2Filter.", "defaultValue": false }, { "name": "spring.datasource.druid.filter.commons-log.enabled", "type": "java.lang.Boolean", + "sourceType": "com.alibaba.druid.spring.boot.autoconfigure.stat.DruidFilterConfiguration", "description": "Enable CommonsLogFilter.", "defaultValue": false }, { "name": "spring.datasource.druid.filter.wall.enabled", "type": "java.lang.Boolean", + "sourceType": "com.alibaba.druid.spring.boot.autoconfigure.stat.DruidFilterConfiguration", "description": "Enable WallFilter.", "defaultValue": false } From 56d15618b05a0425f9738b67fc4065c59bad9068 Mon Sep 17 00:00:00 2001 From: lihengming <89921218@qq.com> Date: Tue, 29 Aug 2017 19:12:48 +0800 Subject: [PATCH 2/2] add > embedded database default config support for # 1929 --- druid-spring-boot-starter/README.md | 2 +- druid-spring-boot-starter/README_EN.md | 2 +- .../boot/autoconfigure/DruidDataSourceWrapper.java | 12 ++++++------ .../src/test/resources/application.properties | 11 +++-------- 4 files changed, 11 insertions(+), 16 deletions(-) diff --git a/druid-spring-boot-starter/README.md b/druid-spring-boot-starter/README.md index 9ee2892f41..fb62a2ad86 100644 --- a/druid-spring-boot-starter/README.md +++ b/druid-spring-boot-starter/README.md @@ -25,7 +25,7 @@ Druid Spring Boot Starter 用于帮助你在Spring Boot项目中轻松集成Drui spring.datasource.url= spring.datasource.username= spring.datasource.password= - # ...其他配置(可选,不是必须的) + # ...其他配置(可选,不是必须的,使用内嵌数据库的话上述三项也可省略不填) ``` ## 配置属性 diff --git a/druid-spring-boot-starter/README_EN.md b/druid-spring-boot-starter/README_EN.md index 344a29928e..395eb37ef0 100644 --- a/druid-spring-boot-starter/README_EN.md +++ b/druid-spring-boot-starter/README_EN.md @@ -24,7 +24,7 @@ Spring Boot with Druid support, help you simplify Druid config in Spring Boot. spring.datasource.url= spring.datasource.username= spring.datasource.password= - # ...other config (optional) + # ...other config (optional, use the embedded database, then the above three items can also be omitted) ``` ## Configuration Properties Druid Spring Boot Starter properties name in full compliance with Druid configuration, you can configure the Druid database connection pool and monitor the configuration properties through the following configuration properties, using default values if not configured. diff --git a/druid-spring-boot-starter/src/main/java/com/alibaba/druid/spring/boot/autoconfigure/DruidDataSourceWrapper.java b/druid-spring-boot-starter/src/main/java/com/alibaba/druid/spring/boot/autoconfigure/DruidDataSourceWrapper.java index 05d445acc0..d023cf4e7d 100644 --- a/druid-spring-boot-starter/src/main/java/com/alibaba/druid/spring/boot/autoconfigure/DruidDataSourceWrapper.java +++ b/druid-spring-boot-starter/src/main/java/com/alibaba/druid/spring/boot/autoconfigure/DruidDataSourceWrapper.java @@ -26,8 +26,8 @@ import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.wall.WallFilter; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties; import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.core.env.Environment; /** * @author lihengming [89921218@qq.com] @@ -35,22 +35,22 @@ import org.springframework.core.env.Environment; @ConfigurationProperties("spring.datasource.druid") class DruidDataSourceWrapper extends DruidDataSource implements InitializingBean { @Autowired - private Environment env; + private DataSourceProperties sourceProperties; @Override public void afterPropertiesSet() throws Exception { //if not found prefix 'spring.datasource.druid' jdbc properties ,'spring.datasource' prefix jdbc properties will be used. if (super.getUsername() == null) { - super.setUsername(env.getProperty("spring.datasource.username")); + super.setUsername(sourceProperties.determineUsername()); } if (super.getPassword() == null) { - super.setPassword(env.getProperty("spring.datasource.password")); + super.setPassword(sourceProperties.determinePassword()); } if (super.getUrl() == null) { - super.setUrl(env.getProperty("spring.datasource.url")); + super.setUrl(sourceProperties.determineUrl()); } if (super.getDriverClassName() == null) { - super.setDriverClassName(env.getProperty("spring.datasource.driver-class-name")); + super.setDriverClassName(sourceProperties.determineDriverClassName()); } } diff --git a/druid-spring-boot-starter/src/test/resources/application.properties b/druid-spring-boot-starter/src/test/resources/application.properties index a9ab55f8f8..6f583300f2 100644 --- a/druid-spring-boot-starter/src/test/resources/application.properties +++ b/druid-spring-boot-starter/src/test/resources/application.properties @@ -1,4 +1,4 @@ -# 只有下面三个是必填项,其他配置不是必须的 +# 只有下面三个是必填项(使用内嵌数据库的话这三个也可以不用填),其他配置不是必须的 spring.datasource.url=jdbc:h2:file:./demo-db spring.datasource.username=sa spring.datasource.password=sa @@ -11,19 +11,14 @@ spring.datasource.druid.password=sa spring.datasource.druid.initial-size=5 spring.datasource.druid.max-active=10 spring.datasource.druid.min-evictable-idle-time-millis=100000 +spring.datasource.druid.validation-query=select 1 spring.datasource.druid.aop-patterns=com.alibaba.druid.spring.boot.demo.service.* -# 自定义StatFilter 配置 +# 自定义StatFilter 配置 其他 Filter 不再演示 spring.datasource.druid.filter.stat.db-type=h2 spring.datasource.druid.filter.stat.log-slow-sql=true spring.datasource.druid.filter.stat.slow-sql-millis=2000 -# 自定义WallFilter 配置,其他 Filter 不再演示 -spring.datasource.druid.filter.wall.enabled=true -spring.datasource.druid.filter.wall.db-type=h2 -spring.datasource.druid.filter.wall.config.delete-allow=false -spring.datasource.druid.filter.wall.config.drop-table-allow=false - # JPA spring.jpa.show-sql= true spring.jpa.hibernate.ddl-auto=create-drop