baomidou / dynamic-datasource

dynamic datasource for springboot 多数据源 动态数据源 主从分离 读写分离 分布式事务
https://www.kancloud.cn/tracy5546/dynamic-datasource/2264611
Apache License 2.0
4.74k stars 1.19k forks source link

dynamicRoutingDataSource #57

Closed nanzhang237420759 closed 5 years ago

nanzhang237420759 commented 5 years ago

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

nanzhang237420759 commented 5 years ago

目前是这个还不能动态添加数据源么?

huayanYu commented 5 years ago

注入DataSource 然后强转

Okmju commented 5 years ago

我也是这个问题,找到了你写的wiki中“动态增减数据源”,进行了强转,但是其中“DynamicDatasource”类找不到引用,请问该怎么做?

gatspy commented 5 years ago

以前是直接注入DynamicRoutingDataSource, 现在是注入DataSource, 然后强转.

@Autowired
private DataSource dataSource;

public void registeDataSource(){
    DynamicRoutingDataSource dynamicRoutingDataSource = (DynamicRoutingDataSource)dataSource;

    // 获取自定义的datasource
    DataSource dataSource = getDataSource();
    dynamicRoutingDataSource.addDataSource("test", dataSource);

    // ....
}