From 4f67c3001e8239ee14e899a1b2681b5ad2b76f59 Mon Sep 17 00:00:00 2001 From: haocao Date: Wed, 27 Dec 2017 15:26:24 +0800 Subject: [PATCH] Fixed #521. --- RELEASE-NOTES.md | 3 +++ .../core/constant/ShardingProperties.java | 11 ++++++++++- .../core/yaml/sharding/YamlShardingIntegrateTest.java | 4 +++- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/RELEASE-NOTES.md b/RELEASE-NOTES.md index 1f68bd34845b0..dd1eea4876474 100644 --- a/RELEASE-NOTES.md +++ b/RELEASE-NOTES.md @@ -3,6 +3,9 @@ ### 功能提升 1. [ISSUE #475](https://github.com/shardingjdbc/sharding-jdbc/issues/475) 支持create index语句 +### 缺陷修正 +1. [ISSUE #521](https://github.com/shardingjdbc/sharding-jdbc/issues/521) YAML文件中ShardingProperties设置无效 + ## 2.0.1 ### 功能提升 diff --git a/sharding-jdbc-core/src/main/java/io/shardingjdbc/core/constant/ShardingProperties.java b/sharding-jdbc-core/src/main/java/io/shardingjdbc/core/constant/ShardingProperties.java index eb12f31c59ea2..fdf144ba50ec4 100644 --- a/sharding-jdbc-core/src/main/java/io/shardingjdbc/core/constant/ShardingProperties.java +++ b/sharding-jdbc-core/src/main/java/io/shardingjdbc/core/constant/ShardingProperties.java @@ -17,6 +17,7 @@ package io.shardingjdbc.core.constant; +import com.google.common.base.Strings; import io.shardingjdbc.core.util.StringUtil; import com.google.common.base.Joiner; @@ -80,7 +81,15 @@ private String getErrorMessage(final ShardingPropertiesConstant shardingProperti */ @SuppressWarnings("unchecked") public T getValue(final ShardingPropertiesConstant shardingPropertiesConstant) { - String result = props.getProperty(shardingPropertiesConstant.getKey(), shardingPropertiesConstant.getDefaultValue()); + String result = props.getProperty(shardingPropertiesConstant.getKey()); + if (Strings.isNullOrEmpty(result)) { + Object obj = props.get(shardingPropertiesConstant.getKey()); + if (null == obj) { + result = shardingPropertiesConstant.getDefaultValue(); + } else { + result = obj.toString(); + } + } if (boolean.class == shardingPropertiesConstant.getType()) { return (T) Boolean.valueOf(result); } diff --git a/sharding-jdbc-core/src/test/java/io/shardingjdbc/core/yaml/sharding/YamlShardingIntegrateTest.java b/sharding-jdbc-core/src/test/java/io/shardingjdbc/core/yaml/sharding/YamlShardingIntegrateTest.java index 1a4ee2d1a6b96..8e7baf3adc760 100644 --- a/sharding-jdbc-core/src/test/java/io/shardingjdbc/core/yaml/sharding/YamlShardingIntegrateTest.java +++ b/sharding-jdbc-core/src/test/java/io/shardingjdbc/core/yaml/sharding/YamlShardingIntegrateTest.java @@ -23,6 +23,7 @@ import io.shardingjdbc.core.api.ConfigMapContext; import io.shardingjdbc.core.api.ShardingDataSourceFactory; import io.shardingjdbc.core.constant.ShardingProperties; +import io.shardingjdbc.core.constant.ShardingPropertiesConstant; import io.shardingjdbc.core.yaml.AbstractYamlDataSourceTest; import lombok.RequiredArgsConstructor; import org.junit.Test; @@ -44,6 +45,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertTrue; @RunWith(Parameterized.class) @RequiredArgsConstructor @@ -83,7 +85,7 @@ public DataSource apply(final String key) { field.setAccessible(true); } ShardingProperties shardingProperties = (ShardingProperties) field.get(dataSource); - //assertTrue((Boolean) shardingProperties.getValue(ShardingPropertiesConstant.SQL_SHOW)); + assertTrue((Boolean) shardingProperties.getValue(ShardingPropertiesConstant.SQL_SHOW)); } Map configMap = new ConcurrentHashMap<>(); configMap.put("key1", "value1");