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) { public ConnectionSource createConnectionSource(@Inject DataSource dataSource) {
DataSource[] slaves = null; DataSource[] slaves = null;
if (Lang.loadClassQuite("org.nutz.boot.starter.jdbc.DataSourceStarter") != 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) if (slaveDataSource != null)
slaves = new DataSource[] {slaveDataSource}; slaves = new DataSource[] {slaveDataSource};
} }

View File

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

View File

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