@Componentpublic class MultiDataSource extends AbstractRoutingDataSource {@Autowiredprivate Dydatasource dydatasource;private static final ThreadLocal<String> dataSourceKey = new InheritableThreadLocal<String>();/*** 設(shè)置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();}/*** 返回當(dāng)前dataSourceKey的值*/@Overrideprotected Object determineCurrentLookupKey() {// return dataSourceKey.get();return null;}}
1 回答

慕虎7371278
TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超4個(gè)贊
這種情況,幫你找到兩種解決辦法:
1 .將接口與對(duì)應(yīng)的實(shí)現(xiàn)類放在與application啟動(dòng)類的同一個(gè)目錄或者他的子目錄下,這樣注解可以被掃描到,這是最省事的辦法
2 .在指定的application類上加上這么一行注解,手動(dòng)指定application類要掃描哪些包下的注解
添加回答
舉報(bào)
0/150
提交
取消