使用的插件https://github.com/nutzam/nutzmore/tree/master/nutz-plugins-iocloader
自定义ioc
public class DynamicDataSourceIocLoader extends JsonLoader {
@Inject
DatabaseSettingService databaseSettingService;
@Override
public IocObject load(IocLoading loading, String name) throws ObjectLoadException {
int databaseSettingId =0;
if(!name.startsWith("dynamicDataSourceDao_")) {
}else {
int lastIndex = name.lastIndexOf("_");
if (lastIndex > -1) {
databaseSettingId = Integer.parseInt(name.substring(lastIndex + 1));
}
}
String dynamicDataSourceDaoId = "dynamicDataSourceDao_" + databaseSettingId;
if (getMap().containsKey(dynamicDataSourceDaoId))
return super.load(loading, dynamicDataSourceDaoId);
Map<String, Object> map = null;
try {
String dynamicDataSourceConfigFactoryId = "dynamicDataSourceConfigFactory_" + databaseSettingId;
String tpl =""/**~!{
{
$dynamicDataSourceConfigFactoryId : {
type : "ren.spider.DynamicDataSourceConfigFactory",
args : [ $databaseSettingId ]
}
}
}*/
;
System.out.println(tpl);
map = Json.fromJsonAsMap(Object.class, tpl);
name = dynamicDataSourceConfigFactoryId;
getMap().put(dynamicDataSourceConfigFactoryId, map);
IocObject iocObject=super.load(loading, dynamicDataSourceConfigFactoryId);
System.out.println(iocObject);
String dataSourceConfigId = "dataSourceConfig_" + databaseSettingId;
tpl = ""/**~!{
{
$dataSourceConfigId : {
factory : '@$dynamicDataSourceConfigFactoryId#create'
}
}
}*/
;
tpl = tpl.replace("@", "$");
System.out.println(tpl);
map = Json.fromJsonAsMap(Object.class, tpl);
name = dataSourceConfigId;
getMap().put(dataSourceConfigId, map);
iocObject=super.load(loading, dataSourceConfigId);
System.out.println(iocObject);
String dynamicDataSourceId = "dynamicDataSource_" + databaseSettingId;
tpl = ""/**~!{{
$dynamicDataSourceId : {
type : "com.zaxxer.hikari.HikariDataSource",
events : {
depose : 'close'
},
args : [ {
refer : "$dataSourceConfigId"
} ]
}
}}*/
;
System.out.println(tpl);
map = Json.fromJsonAsMap(Object.class, tpl);
name = dynamicDataSourceId;
getMap().put(dynamicDataSourceId, map);
iocObject=super.load(loading, dynamicDataSourceId);
tpl = ""/**~!{{
$dynamicDataSourceDaoId : {
type : "org.nutz.dao.impl.NutDao",
args : [ {
refer : "$dynamicDataSourceId"
} ]
}
}}*/
;
System.out.println(tpl);
map = Json.fromJsonAsMap(Object.class, tpl);
name = dynamicDataSourceDaoId;
getMap().put(dynamicDataSourceDaoId, map);
iocObject=super.load(loading, dynamicDataSourceDaoId);
return iocObject;
} catch (Throwable e) {
getMap().remove(name);
throw Lang.wrapThrow(e);
}
}
@Override
public boolean has(String name) {
/*
if (name.startsWith("dynamicDataSourceDao_")) {
System.out.println(databaseSettingService);
int lastIndex = name.lastIndexOf("_");
if (lastIndex > -1) {
int databaseSettingId = Integer.parseInt(name.substring(lastIndex + 1));
return databaseSettingService.exists(databaseSettingId);
}
}*/
return true;
}
@Override
public String[] getName() {
List<DatabaseSettingDo> res = databaseSettingService.list();
if (res == null || res.isEmpty())
return new String[0];
String[] names = new String[res.size()];
for (int i = 0; i < res.size(); i++) {
names[i] = "dynamicDataSourceDao_" + res.get(i).getId();
}
return names;
}
}
目前遇到的问题是,注入的json结构貌似只能按照json配置文件那种结构,否则注入,是否有其他方式注入,直接把对象注入进去,当然上面的方案代码,目前执行报错,运行失败的。