NutzCN Logo
问答 想实现动态加载数据源到ioc容器里,交由ioc管理,目前遇到的问题
发布于 1959天前 作者 邓华锋 1760 次浏览 复制 上一个帖子 下一个帖子
标签:

使用的插件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配置文件那种结构,否则注入,是否有其他方式注入,直接把对象注入进去,当然上面的方案代码,目前执行报错,运行失败的。

9 回复

has 返回 true 必死无疑啊...

代码过不去 就返回true了

其实你要测试的话, 直接写testcase就方便测试了

NutIoc ioc = new NutIoc(new MyIocLoader(xxx));

好的 那注入对象方法的优雅方式有不

ioc.addBean 方法试一下?

好的 我试试 主要是想实现自定义的ioc 这样方便管理

$dynamicDataSourceId : {
				type : "com.zaxxer.hikari.HikariDataSource",
				events : {
					depose : 'close'
				},
				args : [ {
					refer : "$dataSourceConfigId"
				} ]

最主要的是想ioc在销毁时调用数据源的close方法

看看AnnoIocLoader的做法

添加回复
请先登陆
回到顶部