Closed nanzhang237420759 closed 5 years ago
目前是这个还不能动态添加数据源么?
注入DataSource 然后强转
我也是这个问题,找到了你写的wiki中“动态增减数据源”,进行了强转,但是其中“DynamicDatasource”类找不到引用,请问该怎么做?
以前是直接注入DynamicRoutingDataSource
, 现在是注入DataSource, 然后强转.
@Autowired
private DataSource dataSource;
public void registeDataSource(){
DynamicRoutingDataSource dynamicRoutingDataSource = (DynamicRoutingDataSource)dataSource;
// 获取自定义的datasource
DataSource dataSource = getDataSource();
dynamicRoutingDataSource.addDataSource("test", dataSource);
// ....
}
Enviroment
JDK Version(required): 1.8
SpringBoot Version(required): 2.1.0.RELEASE
Starter Version(required): 2.5.3
Describe
@Autowired //在需要的地方注入,然后调用相关方法增减数据源 private DynamicRoutingDataSource dynamicRoutingDataSource;
在控制器中注入了,启动项目报错
` Field dynamicRoutingDataSource in com.baomidou.samples.druid.mybatis.controller.IndexController required a bean of type 'com.baomidou.dynamic.datasource.DynamicRoutingDataSource' that could not be found.
The injection point has the following annotations:
Action:
Consider defining a bean of type 'com.baomidou.dynamic.datasource.DynamicRoutingDataSource' in your configuration. `
Expected Result:
Actual Result:
Steps to reproduce
Step 1
Step 2
Step 3