@Componentpublic class MultiDataSource extends AbstractRoutingDataSource {@Autowiredprivate Dydatasource dydatasource;private static final ThreadLocal<String> dataSourceKey = new InheritableThreadLocal<String>();/*** 设置dataSourceKey的值* @param dataSource*/public void setDataSourceKey(String dataSource) throws SQLException {{System.out.println(22222);System.out.println(dataSource);// String conf = "applicationContext.xml";// ApplicationContext ac = new ClassPathXmlApplicationContext(conf);// Dydatasource dydatasource = ac.getBean("dydatasource", Dydatasource.class);DataSource turedatasource = dydatasource.registerDs(dataSource);System.out.println(dydatasource);Object ture = turedatasource;Object data = dataSource;System.out.println(2);Map<Object, Object> objectObjectMap = new HashMap<Object, Object>();objectObjectMap.put(data, ture);super.setTargetDataSources(objectObjectMap);System.out.println(objectObjectMap.get(data));// dataSourceKey.set(dataSource);}}/**+* 清除dataSourceKey的值*/public static void toDefault() {// dataSourceKey.remove();}/*** 返回当前dataSourceKey的值*/@Overrideprotected Object determineCurrentLookupKey() {// return dataSourceKey.get();return null;}}
1 回答
慕虎7371278
TA贡献1802条经验 获得超4个赞
这种情况,帮你找到两种解决办法:
1 .将接口与对应的实现类放在与application启动类的同一个目录或者他的子目录下,这样注解可以被扫描到,这是最省事的办法
2 .在指定的application类上加上这么一行注解,手动指定application类要扫描哪些包下的注解
添加回答
举报
0/150
提交
取消