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); + } }