Skip to content

Commit 2977c62

Browse files
committed
refactor: 使用 Assert 检查数据源配置
1 parent fe13610 commit 2977c62

1 file changed

Lines changed: 4 additions & 8 deletions

File tree

simple-framework/src/main/java/top/cadecode/framework/config/DataSourceConfig.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.springframework.core.env.Environment;
1111
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
1212
import org.springframework.transaction.PlatformTransactionManager;
13+
import org.springframework.util.Assert;
1314
import top.cadecode.common.core.datasource.DynamicDataSource;
1415
import top.cadecode.common.core.datasource.DynamicDataSourceHolder;
1516

@@ -60,9 +61,7 @@ public DynamicDataSource dynamicDataSource() {
6061
}
6162
});
6263
// 判断是否设置默认数据源
63-
if (!dynamicDS.hasDefaultDataSource()) {
64-
throw new IllegalArgumentException("未指定默认数据源");
65-
}
64+
Assert.isTrue(dynamicDS.hasDefaultDataSource(), "未指定默认数据源");
6665
// 设置数据源到 dynamicDataSource
6766
dynamicDS.setTargetDataSources(dataSourceMap);
6867
return dynamicDS;
@@ -82,17 +81,14 @@ private DataSource getDataSource(String type, String name) {
8281
try {
8382
dataSourceClass = (Class<DataSource>) Class.forName(type);
8483
// 判断是否是 DataSource 类型
85-
if (!DataSource.class.isAssignableFrom(dataSourceClass)) {
86-
throw new IllegalArgumentException("数据源 " + name + " 的类型 " + type + " 不合适");
87-
}
84+
boolean isDataSource = DataSource.class.isAssignableFrom(dataSourceClass);
85+
Assert.isTrue(isDataSource, "数据源 " + name + " 的类型 " + type + " 不合适");
8886
// 生成数据源实例
8987
return Binder.get(environment)
9088
.bind(DBS_PREFIX + name + DBS_SUFFIX, dataSourceClass)
9189
.get();
9290
} catch (ClassNotFoundException e) {
9391
throw new IllegalArgumentException("数据源 " + name + " 的类型 " + type + " 不存在");
94-
} catch (Exception e) {
95-
throw new IllegalArgumentException("创建数据源 " + name + " 时出现绑定错误");
9692
}
9793
}
9894

0 commit comments

Comments
 (0)