diff --git a/.travis.yml b/.travis.yml
index 64dfcf5..a5de278 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -7,4 +7,4 @@ cache:
directories:
- $HOME/.m2/repository
after_success:
- - cd druid-spring-boot-samples && mvn clean -Pcoveralls cobertura:cobertura coveralls:report
\ No newline at end of file
+ - cd druid-spring-boot-example && mvn clean -Pcoveralls cobertura:cobertura coveralls:report
\ No newline at end of file
diff --git a/README.md b/README.md
index 729687a..47364ad 100644
--- a/README.md
+++ b/README.md
@@ -14,16 +14,14 @@ Druid Spring Boot Starter 将帮助你在 Spring Boot 中使用 Druid。
com.github.drtrang
druid-spring-boot-starter
- 1.1.1
+ 1.1.2
```
## NEW !
-1. 新增全配置说明 [druid.yml](https://github.com/drtrang/druid-spring-boot/blob/master/druid-spring-boot-samples/src/main/resources/druid.yml)
-1. 新增 `statement-sql-format-option` 的代码提示
+1. 改进多数据源的声明方式,由 Starter 自动发现配置并注册到 Spring Context,详情请查看 [Druid 多数据源支持.md](https://github.com/drtrang/druid-spring-boot/tree/master/docs/Druid%20%E5%A4%9A%E6%95%B0%E6%8D%AE%E6%BA%90%E6%94%AF%E6%8C%81.md)。
+1. 新增全配置说明 [druid.yml](https://github.com/drtrang/druid-spring-boot/blob/master/druid-spring-boot-example/druid-spring-boot-mybatis-example/src/main/resources/druid.yml)
2. 新增 `druid-spring-boot-actuator-starter`
-3. 新增 ConfigFilter 的自动配置,替换 Druid 默认的 `connectionProperties` 方式
-4. 完美支持多数据源 [ISSUE #2](https://github.com/drtrang/druid-spring-boot/issues/2)
## 配置
@@ -45,6 +43,10 @@ Druid Spring Boot Starter 会将以 `spring.datasource.druid` 为前缀的配置
```yaml
spring:
datasource:
+ driver-class-name: org.h2.Driver
+ url: jdbc:h2:file:./samples
+ username: root
+ password: 123456
druid:
initial-size: 1
min-idle: 1
@@ -91,13 +93,24 @@ spring:
```
### 多数据源
-1.0.2 版本新增多数据源支持,使用方式请查看 [Druid 多数据源支持.md](https://github.com/drtrang/druid-spring-boot/tree/master/docs/Druid%20%E5%A4%9A%E6%95%B0%E6%8D%AE%E6%BA%90%E6%94%AF%E6%8C%81.md)。
+1.1.2 版本改进了多数据源的声明方式,由 Starter 自动发现配置并注册到 Spring Context,详情请查看 [Druid 多数据源支持.md](https://github.com/drtrang/druid-spring-boot/tree/master/docs/Druid%20%E5%A4%9A%E6%95%B0%E6%8D%AE%E6%BA%90%E6%94%AF%E6%8C%81.md)。
+
+```yaml
+spring:
+ datasource:
+ druid:
+ data-sources:
+ first:
+ url: jdbc:h2:file:./first
+ second:
+ url: jdbc:h2:file:./second
+```
### 配置示例
-[application.yml](https://github.com/drtrang/druid-spring-boot/blob/master/druid-spring-boot-samples/src/main/resources/application.yml)
+[application.yml](https://github.com/drtrang/druid-spring-boot/blob/master/druid-spring-boot-example/druid-spring-boot-mybatis-example/src/main/resources/application.yml)
### 全配置说明
-[druid.yml](https://github.com/drtrang/druid-spring-boot/blob/master/druid-spring-boot-samples/src/main/resources/druid.yml)
+[druid.yml](https://github.com/drtrang/druid-spring-boot/blob/master/druid-spring-boot-example/druid-spring-boot-mybatis-example/src/main/resources/druid.yml)
## 自动提示
@@ -114,7 +127,7 @@ Druid Spring Boot Starter 基于 `spring-boot-configuration-processor` 模块,
## 演示
-[druid-spring-boot-samples](https://github.com/drtrang/druid-spring-boot/tree/master/druid-spring-boot-samples) 演示了 Druid Spring Boot Starter 的使用方式,可以作为参考。
+[druid-spring-boot-example](https://github.com/drtrang/druid-spring-boot/tree/master/druid-spring-boot-example) 演示了 Druid Spring Boot Starter 的使用方式,可以作为参考。
## Change Log
diff --git a/docs/Druid 多数据源支持.md b/docs/Druid 多数据源支持.md
index a46d4c3..36f9527 100644
--- a/docs/Druid 多数据源支持.md
+++ b/docs/Druid 多数据源支持.md
@@ -1,20 +1,33 @@
# Druid 多数据源支持
-1.0.2 版本新增支持多数据源的 **DruidDataSource2**,开启该特性需要手动声明数据源,并指定注入的前缀 `spring.datasource.druid.${name}`,其中 `${name}` 可根据实际情况自定义。
+1.0.2 版本新增了支持多数据源的 **DruidDataSource2**,可以将 `spring.datasource` 和 `spring.datasource.druid` 开头的配置注入到数据源,解决各数据源之间的相同配置问题。
+1.1.2 版本改进了多数据源的注册方式,由 Starter 自动发现配置并注册到 Spring Context,用户只需在配置文件中声明即可。
## 使用方式
-1. 声明各个 DataSource,类型为 `com.github.trang.druid.autoconfigure.datasource.DruidDataSource2`。
- ```java
- @Bean
- @ConfigurationProperties("spring.datasource.druid.one")
- public DruidDataSource firstDataSource() {
- return new DruidDataSource2();
- }
+1. 编辑配置文件,Starter 会将 `spring.datasource.druid.data-sources` 开头的的属性注入到 Map 中,从而构建多数据源。
+ ```yaml
+ spring:
+ autoconfigure:
+ exclude:
+ - org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
+ datasource:
+ driver-class-name: org.h2.Driver
+ username: root
+ password: 123456
+ druid:
+ # 多数据源的标识,若该属性存在则为多数据源环境
+ data-sources:
+ ## master 数据源的配置,以下为 master 数据源独有的配置
+ master:
+ url: jdbc:h2:file:./master
+ ## slave 数据源的配置,以下为 slave 数据源独有的配置
+ slave:
+ url: jdbc:h2:file:./slave
```
-2. 手动排除 `org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration`,以下方式任选其一:
+注: 排除 `org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration` 有两种方式,以下方式任选其一即可:
1. 在 `@SpringBootApplication` 注解中排除
```java
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@@ -29,46 +42,39 @@
## 原理
-DruidDataSourceSupport 类存在的目的是为了注入 `spring.datasource.druid` 的配置。
-基于 Spring4 的特性,DruidDataSource2 在继承 DruidDataSourceSupport 的同时,也会继承这些配置,由此解决多数据源场景下相同配置重复定义的问题。
+DruidDataSource2Support 类存在的目的是为了注入 `spring.datasource` 和 `spring.datasource.druid` 的配置。
+基于 Spring4 的特性,DruidDataSource2 在继承 DruidDataSource2Support 的同时,也会继承这些配置,由此解决多数据源场景下相同配置重复定义的问题。
也就是说,以下两种方式是等价的:
```yaml
spring:
datasource:
druid:
- min-evictable-idle-time-millis: 1800000
- max-evictable-idle-time-millis: 25200000
- one:
- name: one
- two:
- name: two
----
-spring:
- datasource:
- druid:
- one:
- name: one
- min-evictable-idle-time-millis: 1800000
- max-evictable-idle-time-millis: 25200000
- two:
- name: two
- min-evictable-idle-time-millis: 1800000
- max-evictable-idle-time-millis: 25200000
+ driver-class-name: org.h2.Driver
+ username: root
+ password: 123456
+ data-sources:
+ master:
+ url: jdbc:h2:file:./master
+ slave:
+ url: jdbc:h2:file:./slave
```
-若子数据源有相同的配置时,则会覆盖掉父数据源的值:
```yaml
spring:
datasource:
druid:
- max-active: 20
- one:
- name: one
- max-active: 50
- two:
- name: two
+ master:
+ driver-class-name: org.h2.Driver
+ username: root
+ password: 123456
+ url: jdbc:h2:file:./master
+ slave:
+ driver-class-name: org.h2.Driver
+ username: root
+ password: 123456
+ url: jdbc:h2:file:./slave
```
## 演示
-将 [druid-spring-boot-samples](https://github.com/drtrang/druid-spring-boot/tree/master/druid-spring-boot-samples) 中 [application.yml](https://github.com/drtrang/druid-spring-boot/blob/master/druid-spring-boot-samples/src/main/resources/application.yml) 的 `spring.profiles.active` 配置改为 `dynamic` 即可。
\ No newline at end of file
+将 [druid-spring-boot-example](https://github.com/drtrang/druid-spring-boot/blob/master/druid-spring-boot-example/druid-spring-boot-mybatis-example/src/main/resources/application.yml) 的 `spring.profiles.active` 配置改为 `dynamic` 即可。
\ No newline at end of file
diff --git a/druid-spring-boot-actuator/druid-spring-boot-actuator-autoconfigure/pom.xml b/druid-spring-boot-actuator/druid-spring-boot-actuator-autoconfigure/pom.xml
index 04e9d39..556efec 100644
--- a/druid-spring-boot-actuator/druid-spring-boot-actuator-autoconfigure/pom.xml
+++ b/druid-spring-boot-actuator/druid-spring-boot-actuator-autoconfigure/pom.xml
@@ -7,7 +7,7 @@
com.github.drtrang
druid-spring-boot-actuator
- 1.1.2-SNAPSHOT
+ 1.1.2
druid-spring-boot-actuator-autoconfigure
diff --git a/druid-spring-boot-actuator/druid-spring-boot-actuator-starter/pom.xml b/druid-spring-boot-actuator/druid-spring-boot-actuator-starter/pom.xml
index 1b00e98..0c9da63 100644
--- a/druid-spring-boot-actuator/druid-spring-boot-actuator-starter/pom.xml
+++ b/druid-spring-boot-actuator/druid-spring-boot-actuator-starter/pom.xml
@@ -7,7 +7,7 @@
com.github.drtrang
druid-spring-boot-actuator
- 1.1.2-SNAPSHOT
+ 1.1.2
druid-spring-boot-actuator-starter
diff --git a/druid-spring-boot-actuator/pom.xml b/druid-spring-boot-actuator/pom.xml
index 897e06e..c7b7ebe 100644
--- a/druid-spring-boot-actuator/pom.xml
+++ b/druid-spring-boot-actuator/pom.xml
@@ -7,7 +7,7 @@
com.github.drtrang
druid-spring-boot-parent
- 1.1.2-SNAPSHOT
+ 1.1.2
druid-spring-boot-actuator
diff --git a/druid-spring-boot-example/druid-spring-boot-mybatis-example/README.md b/druid-spring-boot-example/druid-spring-boot-mybatis-example/README.md
index 1e0c506..952edbc 100644
--- a/druid-spring-boot-example/druid-spring-boot-mybatis-example/README.md
+++ b/druid-spring-boot-example/druid-spring-boot-mybatis-example/README.md
@@ -1,5 +1,5 @@
-# Druid Spring Boot Samples
+# Druid Spring Boot Example
-Druid Spring Boot Samples 项目演示了 Druid Spring Boot Starter 的使用方式。将项目导入 IDE 后,直接运行 `com.github.trang.druid.DruidSpringBootStarterApplication#main()` 即可,无需依赖其它环境。
+Druid Spring Boot Example 项目演示了 Druid Spring Boot Starter 的使用方式。将项目导入 IDE 后,直接运行 `com.github.trang.druid.DruidSpringBootStarterApplication#main()` 即可,无需依赖其它环境。
-项目默认使用单数据源,将 [application.yml](https://github.com/drtrang/druid-spring-boot/blob/master/druid-spring-boot-samples/src/main/resources/application.yml) 的 `spring.profiles.active` 配置改为 `dynamic` 即可切换为多数据源演示。
\ No newline at end of file
+项目默认使用单数据源,将 [application.yml](https://github.com/drtrang/druid-spring-boot/blob/master/druid-spring-boot-example/druid-spring-boot-mybatis-example/src/main/resources/application.yml) 的 `spring.profiles.active` 配置改为 `dynamic` 即可切换为多数据源演示。
\ No newline at end of file
diff --git a/druid-spring-boot-example/druid-spring-boot-mybatis-example/pom.xml b/druid-spring-boot-example/druid-spring-boot-mybatis-example/pom.xml
index 8dfbe84..3dd44c9 100644
--- a/druid-spring-boot-example/druid-spring-boot-mybatis-example/pom.xml
+++ b/druid-spring-boot-example/druid-spring-boot-mybatis-example/pom.xml
@@ -7,7 +7,7 @@
com.github.drtrang
druid-spring-boot-example
- 1.1.2-SNAPSHOT
+ 1.1.2
druid-spring-boot-mybatis-example
diff --git a/druid-spring-boot-example/druid-spring-boot-mybatis-example/src/main/resources/druid.yml b/druid-spring-boot-example/druid-spring-boot-mybatis-example/src/main/resources/druid.yml
index 5a7cdbd..4bd9259 100644
--- a/druid-spring-boot-example/druid-spring-boot-mybatis-example/src/main/resources/druid.yml
+++ b/druid-spring-boot-example/druid-spring-boot-mybatis-example/src/main/resources/druid.yml
@@ -154,10 +154,10 @@ spring:
druid:
### 子数据源也可以自定义配置,与 `spring.datasource` 相同的属性会覆盖 `spring.datasource` 的配置
### 不同的属性只会注入到当前数据源,不会对其它数据源造成影响
- ### 最终该数据源的配置为 `spring.datasource` + `spring.datasource.druid`
- order-master:
- name: orderMaster
- ### 会覆盖 `spring.datasource.url`,但是因为二者的值相同,所以可以不写
- url: jdbc:h2:file:./samples
- order-slave:
- name: orderSlave
\ No newline at end of file
+ ### 最终该数据源的配置为 `spring.datasource.*` + `spring.datasource.druid.*` + `spring.datasource.druid.${name}.*`
+ data-sources:
+ order-master:
+ ### 会覆盖 `spring.datasource.url`
+ url: jdbc:h2:file:./order_master
+ order-slave:
+ url: jdbc:h2:file:./order_slave
\ No newline at end of file
diff --git a/druid-spring-boot-example/pom.xml b/druid-spring-boot-example/pom.xml
index 5a4e759..d3c0f3a 100644
--- a/druid-spring-boot-example/pom.xml
+++ b/druid-spring-boot-example/pom.xml
@@ -7,7 +7,7 @@
com.github.drtrang
druid-spring-boot-parent
- 1.1.2-SNAPSHOT
+ 1.1.2
druid-spring-boot-example
diff --git a/druid-spring-boot/druid-spring-boot-autoconfigure/pom.xml b/druid-spring-boot/druid-spring-boot-autoconfigure/pom.xml
index 10736f2..ce4fd39 100644
--- a/druid-spring-boot/druid-spring-boot-autoconfigure/pom.xml
+++ b/druid-spring-boot/druid-spring-boot-autoconfigure/pom.xml
@@ -7,7 +7,7 @@
com.github.drtrang
druid-spring-boot
- 1.1.2-SNAPSHOT
+ 1.1.2
druid-spring-boot-autoconfigure
diff --git a/druid-spring-boot/druid-spring-boot-autoconfigure/src/main/java/com/github/trang/druid/autoconfigure/DruidAutoConfiguration.java b/druid-spring-boot/druid-spring-boot-autoconfigure/src/main/java/com/github/trang/druid/autoconfigure/DruidAutoConfiguration.java
index 747a155..3d3105f 100644
--- a/druid-spring-boot/druid-spring-boot-autoconfigure/src/main/java/com/github/trang/druid/autoconfigure/DruidAutoConfiguration.java
+++ b/druid-spring-boot/druid-spring-boot-autoconfigure/src/main/java/com/github/trang/druid/autoconfigure/DruidAutoConfiguration.java
@@ -154,8 +154,12 @@ public class DruidAutoConfiguration implements BeanFactoryAware, EnvironmentAwar
.getBeanDefinition();
// 注册 BeanDefinition
beanFactory.registerBeanDefinition(dataSourceName, beanDefinition);
- // 注册 Alias
- beanFactory.registerAlias(dataSourceName, dataSourceName + "DataSource");
+ String lowerCaseName = dataSourceName.toLowerCase();
+ // 当 beanName 不是以 datasource 结尾时,增加别名
+ if (!lowerCaseName.endsWith("datasource") && !lowerCaseName.endsWith("data-source")) {
+ // 注册 Alias
+ beanFactory.registerAlias(dataSourceName, dataSourceName + "DataSource");
+ }
// 将 'spring.datasource.druid.data-sources.$dataSourceName' 的配置绑定到 Bean
DruidDataSource2 dataSource = beanFactory.getBean(dataSourceName, DruidDataSource2.class);
PropertySourcesBinder propertySourcesBinder = new PropertySourcesBinder(environment);
diff --git a/druid-spring-boot/druid-spring-boot-autoconfigure/src/main/java/com/github/trang/druid/autoconfigure/properties/DruidDataSourceProperties.java b/druid-spring-boot/druid-spring-boot-autoconfigure/src/main/java/com/github/trang/druid/autoconfigure/properties/DruidDataSourceProperties.java
index a4a4b3e..fabd2ae 100644
--- a/druid-spring-boot/druid-spring-boot-autoconfigure/src/main/java/com/github/trang/druid/autoconfigure/properties/DruidDataSourceProperties.java
+++ b/druid-spring-boot/druid-spring-boot-autoconfigure/src/main/java/com/github/trang/druid/autoconfigure/properties/DruidDataSourceProperties.java
@@ -1,6 +1,6 @@
package com.github.trang.druid.autoconfigure.properties;
-import com.alibaba.druid.pool.DruidDataSource;
+import com.github.trang.druid.autoconfigure.datasource.DruidDataSource2;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
@@ -41,8 +41,7 @@ public class DruidDataSourceProperties {
DruidWebStatProperties webStat = new DruidWebStatProperties();
@NestedConfigurationProperty
DruidAopStatProperties aopStat = new DruidAopStatProperties();
- @NestedConfigurationProperty
- Map dataSources = new HashMap<>(16);
+ Map dataSources = new HashMap<>(16);
@Getter@Setter
public static class DruidConfigFilterProperties {
diff --git a/druid-spring-boot/druid-spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/druid-spring-boot/druid-spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json
index 2910e89..3a259f5 100644
--- a/druid-spring-boot/druid-spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json
+++ b/druid-spring-boot/druid-spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json
@@ -3,88 +3,71 @@
{
"name": "spring.datasource.druid.stat.enabled",
"type": "java.lang.Boolean",
- "description": "Whether or not druid stat-filter are enabled.",
+ "description": "是否开启 StatFilter.",
"defaultValue": true
},
{
"name": "spring.datasource.druid.wall.enabled",
"type": "java.lang.Boolean",
- "description": "Whether or not druid wall-filter are enabled.",
+ "description": "是否开启 WallFilter.",
"defaultValue": false
},
{
"name": "spring.datasource.druid.config.enabled",
"type": "java.lang.Boolean",
- "description": "Whether or not druid config-filter are enabled.",
+ "description": "是否开启 ConfigFilter.",
"defaultValue": false
},
{
"name": "spring.datasource.druid.slf4j.enabled",
"type": "java.lang.Boolean",
- "description": "Whether or not druid slf4j-filter are enabled.",
+ "description": "是否开启 Slf4jLogFilter.",
"defaultValue": false
},
{
"name": "spring.datasource.druid.log4j.enabled",
"type": "java.lang.Boolean",
- "description": "Whether or not druid log4j-filter are enabled.",
+ "description": "是否开启 Log4jFilter.",
"defaultValue": false
},
{
"name": "spring.datasource.druid.log4j2.enabled",
"type": "java.lang.Boolean",
- "description": "Whether or not druid log4j2-filter are enabled.",
+ "description": "是否开启 Log4j2Filter.",
"defaultValue": false
},
{
"name": "spring.datasource.druid.commons-log.enabled",
"type": "java.lang.Boolean",
- "description": "Whether or not druid commons-log-filter are enabled.",
- "defaultValue": false
- },
- {
- "name": "spring.datasource.druid.aop-stat.enabled",
- "type": "java.lang.Boolean",
- "description": "Whether or not druid aop-stat are enabled.",
- "sourceType": "com.github.trang.druid.autoconfigure.properties.DruidDataSourceProperties$DruidAopStatProperties",
- "defaultValue": false
- },
- {
- "name": "spring.datasource.druid.web-stat.enabled",
- "type": "java.lang.Boolean",
- "description": "Whether or not druid web-stat are enabled.",
- "sourceType": "com.github.trang.druid.autoconfigure.properties.DruidDataSourceProperties$DruidWebStatProperties",
- "defaultValue": false
- },
- {
- "name": "spring.datasource.druid.stat-view-servlet.enabled",
- "type": "java.lang.Boolean",
- "description": "Whether or not druid stat-view-servlet are enabled.",
- "sourceType": "com.github.trang.druid.autoconfigure.properties.DruidDataSourceProperties$DruidStatViewServletProperties",
+ "description": "是否开启 CommonsLogFilter.",
"defaultValue": false
},
{
"name": "spring.datasource.druid.slf4j.statement-sql-format-option.upp-case",
"type": "java.lang.Boolean",
"sourceType": "com.alibaba.druid.sql.SQLUtils$FormatOption",
+ "description": "是否开启关键字大写.",
"defaultValue": false
},
{
"name": "spring.datasource.druid.slf4j.statement-sql-format-option.pretty-format",
"type": "java.lang.Boolean",
"sourceType": "com.alibaba.druid.sql.SQLUtils$FormatOption",
+ "description": "是否开启 SQL 格式化.",
"defaultValue": true
},
{
"name": "spring.datasource.druid.slf4j.statement-sql-format-option.desensitize",
"type": "java.lang.Boolean",
"sourceType": "com.alibaba.druid.sql.SQLUtils$FormatOption",
+ "description": "是否开启不敏感表名.",
"defaultValue": false
},
{
"name": "spring.datasource.druid.slf4j.statement-sql-format-option.parameterized",
"type": "java.lang.Boolean",
"sourceType": "com.alibaba.druid.sql.SQLUtils$FormatOption",
+ "description": "是否开启 SQL 参数化.",
"defaultValue": false
}
],
diff --git a/druid-spring-boot/druid-spring-boot-starter/pom.xml b/druid-spring-boot/druid-spring-boot-starter/pom.xml
index 0b98445..2c30f10 100644
--- a/druid-spring-boot/druid-spring-boot-starter/pom.xml
+++ b/druid-spring-boot/druid-spring-boot-starter/pom.xml
@@ -7,7 +7,7 @@
com.github.drtrang
druid-spring-boot
- 1.1.2-SNAPSHOT
+ 1.1.2
druid-spring-boot-starter
diff --git a/druid-spring-boot/pom.xml b/druid-spring-boot/pom.xml
index b4b9f12..405e526 100644
--- a/druid-spring-boot/pom.xml
+++ b/druid-spring-boot/pom.xml
@@ -7,7 +7,7 @@
com.github.drtrang
druid-spring-boot-parent
- 1.1.2-SNAPSHOT
+ 1.1.2
druid-spring-boot
diff --git a/pom.xml b/pom.xml
index 1632fb1..28eb06f 100644
--- a/pom.xml
+++ b/pom.xml
@@ -5,7 +5,7 @@
com.github.drtrang
druid-spring-boot-parent
- 1.1.2-SNAPSHOT
+ 1.1.2
pom
Druid Spring Boot Starter