change: DataSourceStarter.createSlaveDataSource方法添加prefix参数 #116

This commit is contained in:
Wendal Chen 2018-01-28 17:53:40 +08:00
parent 90152b1a47
commit 46497719e1
3 changed files with 6 additions and 6 deletions

View File

@ -75,7 +75,7 @@ public class BeetlSqlStarter {
public ConnectionSource createConnectionSource(@Inject DataSource dataSource) {
DataSource[] slaves = null;
if (Lang.loadClassQuite("org.nutz.boot.starter.jdbc.DataSourceStarter") != null) {
DataSource slaveDataSource = DataSourceStarter.getSlaveDataSource(ioc, conf);
DataSource slaveDataSource = DataSourceStarter.getSlaveDataSource(ioc, conf, "jdbc.slave.");
if (slaveDataSource != null)
slaves = new DataSource[] {slaveDataSource};
}

View File

@ -153,18 +153,18 @@ public class DataSourceStarter {
return new HikariDataSource(new HikariConfig(properties));
}
public static DataSource getSlaveDataSource(Ioc ioc, PropertiesProxy conf) {
public static DataSource getSlaveDataSource(Ioc ioc, PropertiesProxy conf, String prefix) {
if (ioc.has("slaveDataSource")) {
return ioc.get(DataSource.class, "slaveDataSource");
} else {
// 看看有多少从数据库被定义了
List<DataSource> slaveDataSources = new ArrayList<>();
for (String key : conf.keys()) {
if (key.startsWith("jdbc.slave.") && key.endsWith(".url")) {
String slaveName = key.substring("jdbc.slave.".length(), key.length() - ".url".length());
if (key.startsWith(prefix) && key.endsWith(".url")) {
String slaveName = key.substring(prefix.length(), key.length() - ".url".length());
log.debug("found Slave DataSource name=" + slaveName);
try {
DataSource slaveDataSource = DataSourceStarter.createSlaveDataSource(ioc, conf, "jdbc.slave." + slaveName + ".");
DataSource slaveDataSource = DataSourceStarter.createSlaveDataSource(ioc, conf, prefix + slaveName + ".");
slaveDataSources.add(slaveDataSource);
slaves.add(slaveDataSource);
}

View File

@ -175,7 +175,7 @@ public class NutDaoStarter {
dao.setInterceptors(interceptors);
// 看看是不是需要注入从数据库
if (Lang.loadClassQuite("org.nutz.boot.starter.jdbc.DataSourceStarter") != null) {
DataSource slaveDataSource = DataSourceStarter.getSlaveDataSource(ioc, conf);
DataSource slaveDataSource = DataSourceStarter.getSlaveDataSource(ioc, conf, "jdbc.slave.");
if (slaveDataSource != null) {
NutDaoRunner runner = new NutDaoRunner();
runner.setSlaveDataSource(slaveDataSource);