+ jdbc配置项支持shardingsphere及指定配置项

This commit is contained in:
JerryMa1024 2020-08-18 08:58:13 +08:00
parent 4a69e0163a
commit 937d384fe6
2 changed files with 39 additions and 1 deletions

View File

@ -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;
}

View File

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