使用注解,spring中无法注入bean? 5
@ComponentpublicclassMultiDataSourceextendsAbstractRoutingDataSource{@Autowiredprivat...
@Component
public class MultiDataSource extends AbstractRoutingDataSource {
@Autowired
private 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的值
*/
@Override
protected Object determineCurrentLookupKey() {
// return dataSourceKey.get();
return null;
}
} 展开
public class MultiDataSource extends AbstractRoutingDataSource {
@Autowired
private 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的值
*/
@Override
protected Object determineCurrentLookupKey() {
// return dataSourceKey.get();
return null;
}
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询