From 937d384fe692aaf5895a15cca8cb2f9edf862ba8 Mon Sep 17 00:00:00 2001 From: JerryMa1024 Date: Tue, 18 Aug 2020 08:58:13 +0800 Subject: [PATCH] =?UTF-8?q?+=20jdbc=E9=85=8D=E7=BD=AE=E9=A1=B9=E6=94=AF?= =?UTF-8?q?=E6=8C=81shardingsphere=E5=8F=8A=E6=8C=87=E5=AE=9A=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/diboot/core/util/ContextHelper.java | 16 ++++++++++++- .../diboot/core/test/util/PropertiesTest.java | 24 +++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/diboot-core/src/main/java/com/diboot/core/util/ContextHelper.java b/diboot-core/src/main/java/com/diboot/core/util/ContextHelper.java index 22ede98..af2aaa9 100644 --- a/diboot-core/src/main/java/com/diboot/core/util/ContextHelper.java +++ b/diboot-core/src/main/java/com/diboot/core/util/ContextHelper.java @@ -258,7 +258,21 @@ public class ContextHelper implements ApplicationContextAware { } if(jdbcUrl == null){ String master = environment.getProperty("spring.datasource.dynamic.primary"); - jdbcUrl = environment.getProperty("spring.datasource.dynamic.datasource."+master+".url"); + if(master != null){ + jdbcUrl = environment.getProperty("spring.datasource.dynamic.datasource."+master+".url"); + } + } + if(jdbcUrl == null){ + String names = environment.getProperty("spring.shardingsphere.datasource.names"); + if(names != null){ + jdbcUrl = environment.getProperty("spring.shardingsphere.datasource."+ names.split(",")[0] +".url"); + } + } + if(jdbcUrl == null){ + String urlConfigItem = environment.getProperty("diboot.datasource.url.config"); + if(urlConfigItem != null){ + jdbcUrl = environment.getProperty(urlConfigItem); + } } return jdbcUrl; } diff --git a/diboot-core/src/test/java/diboot/core/test/util/PropertiesTest.java b/diboot-core/src/test/java/diboot/core/test/util/PropertiesTest.java index 5f70de0..4a27c15 100644 --- a/diboot-core/src/test/java/diboot/core/test/util/PropertiesTest.java +++ b/diboot-core/src/test/java/diboot/core/test/util/PropertiesTest.java @@ -21,7 +21,9 @@ import diboot.core.test.config.SpringMvcConfig; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.core.env.Environment; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner; @@ -36,6 +38,9 @@ import org.springframework.test.context.junit4.SpringRunner; @SpringBootTest(classes = {StartupApplication.class}) public class PropertiesTest { + @Autowired + private Environment environment; + @Test public void testGetString(){ String str1 = PropertiesUtils.get("spring.datasource.url"); @@ -49,4 +54,23 @@ public class PropertiesTest { Integer num = PropertiesUtils.getInteger("spring.datasource.hikari.maximum-pool-size"); Assert.assertTrue(num > 0 ); } + + @Test + public void testDatasourceUrl(){ + String jdbcUrl = null; + if(jdbcUrl == null){ + String names = environment.getProperty("spring.shardingsphere.datasource.names"); + if(names != null){ + jdbcUrl = environment.getProperty("spring.shardingsphere.datasource."+ names.split(",")[0] +".url"); + } + } + if(jdbcUrl == null){ + String urlConfigItem = environment.getProperty("diboot.datasource.url.config"); + if(urlConfigItem != null){ + jdbcUrl = environment.getProperty(urlConfigItem); + } + } + System.out.println(jdbcUrl); + Assert.assertTrue(jdbcUrl != null); + } }