Commit 2d992db62afdc059989343700cbe2fe40028a768
1 parent
687e67db
修复多数据源下enabled=false导致读取不到bean导致异常问题
Showing
1 changed file
with
22 additions
and
2 deletions
ruoyi/src/main/java/com/ruoyi/framework/config/DruidConfig.java
| ... | ... | @@ -19,6 +19,7 @@ import com.alibaba.druid.pool.DruidDataSource; |
| 19 | 19 | import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder; |
| 20 | 20 | import com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties; |
| 21 | 21 | import com.alibaba.druid.util.Utils; |
| 22 | +import com.ruoyi.common.utils.spring.SpringUtils; | |
| 22 | 23 | import com.ruoyi.framework.aspectj.lang.enums.DataSourceType; |
| 23 | 24 | import com.ruoyi.framework.config.properties.DruidProperties; |
| 24 | 25 | import com.ruoyi.framework.datasource.DynamicDataSource; |
| ... | ... | @@ -50,13 +51,32 @@ public class DruidConfig |
| 50 | 51 | |
| 51 | 52 | @Bean(name = "dynamicDataSource") |
| 52 | 53 | @Primary |
| 53 | - public DynamicDataSource dataSource(DataSource masterDataSource, DataSource slaveDataSource) | |
| 54 | + public DynamicDataSource dataSource(DataSource masterDataSource) | |
| 54 | 55 | { |
| 55 | 56 | Map<Object, Object> targetDataSources = new HashMap<>(); |
| 56 | 57 | targetDataSources.put(DataSourceType.MASTER.name(), masterDataSource); |
| 57 | - targetDataSources.put(DataSourceType.SLAVE.name(), slaveDataSource); | |
| 58 | + setDataSource(targetDataSources, DataSourceType.SLAVE.name(), "slaveDataSource"); | |
| 58 | 59 | return new DynamicDataSource(masterDataSource, targetDataSources); |
| 59 | 60 | } |
| 61 | + | |
| 62 | + /** | |
| 63 | + * 设置数据源 | |
| 64 | + * | |
| 65 | + * @param targetDataSources 备选数据源集合 | |
| 66 | + * @param sourceName 数据源名称 | |
| 67 | + * @param beanName bean名称 | |
| 68 | + */ | |
| 69 | + public void setDataSource(Map<Object, Object> targetDataSources, String sourceName, String beanName) | |
| 70 | + { | |
| 71 | + try | |
| 72 | + { | |
| 73 | + DataSource dataSource = SpringUtils.getBean(beanName); | |
| 74 | + targetDataSources.put(sourceName, dataSource); | |
| 75 | + } | |
| 76 | + catch (Exception e) | |
| 77 | + { | |
| 78 | + } | |
| 79 | + } | |
| 60 | 80 | |
| 61 | 81 | /** |
| 62 | 82 | * 去除监控页面底部的广告 | ... | ... |