|
1 | 1 | package com.github.cadecode.uniboot.common.plugin.datasource.config; |
2 | 2 |
|
3 | | -import com.github.cadecode.uniboot.common.plugin.datasource.dynamic.DynamicDS; |
4 | | -import com.github.cadecode.uniboot.common.plugin.datasource.dynamic.DynamicDSHolder; |
5 | | -import com.github.cadecode.uniboot.common.plugin.datasource.exception.DynamicDSException; |
| 3 | +import com.github.cadecode.uniboot.common.plugin.datasource.dynamic.DynamicDs; |
| 4 | +import com.github.cadecode.uniboot.common.plugin.datasource.dynamic.DynamicDsHolder; |
| 5 | +import com.github.cadecode.uniboot.common.plugin.datasource.exception.DynamicDsException; |
6 | 6 | import com.zaxxer.hikari.HikariDataSource; |
7 | 7 | import lombok.RequiredArgsConstructor; |
8 | 8 | import lombok.extern.slf4j.Slf4j; |
|
25 | 25 | @Slf4j |
26 | 26 | @RequiredArgsConstructor |
27 | 27 | @Configuration |
28 | | -@EnableConfigurationProperties(DynamicDSProperties.class) |
| 28 | +@EnableConfigurationProperties(DynamicDsProperties.class) |
29 | 29 | @ConditionalOnProperty(name = "uni-boot.dynamic-ds.enable", havingValue = "true") |
30 | | -public class DynamicDSAutoConfig { |
| 30 | +public class DynamicDsAutoConfig { |
31 | 31 |
|
32 | | - private final DynamicDSProperties properties; |
| 32 | + private final DynamicDsProperties properties; |
33 | 33 |
|
34 | 34 | @Bean |
35 | | - public DynamicDS dynamicDS() { |
| 35 | + public DynamicDs dynamicDs() { |
36 | 36 | log.info("Checking dynamic datasource config"); |
37 | | - checkDynamicDSConfig(); |
| 37 | + checkDynamicDsConfig(); |
38 | 38 | log.info("Starting to create dynamic datasource"); |
39 | 39 | // 创建数据源 |
40 | 40 | Map<Object, Object> dataSourceMap; |
41 | 41 | try { |
42 | 42 | dataSourceMap = properties.getDatasource().entrySet().stream() |
43 | 43 | .collect(Collectors.toMap(Entry::getKey, e -> new HikariDataSource(e.getValue()))); |
44 | 44 | } catch (Exception e) { |
45 | | - throw new DynamicDSException("Create dynamic datasource fail", e); |
| 45 | + throw new DynamicDsException("Create dynamic datasource fail", e); |
46 | 46 | } |
47 | | - DynamicDS dynamicDS = new DynamicDS(); |
| 47 | + DynamicDs dynamicDs = new DynamicDs(); |
48 | 48 | // 添加数据源 |
49 | | - dynamicDS.setTargetDataSources(dataSourceMap); |
| 49 | + dynamicDs.setTargetDataSources(dataSourceMap); |
50 | 50 | // 设置默认数据源 |
51 | | - dynamicDS.setDefaultTargetDataSource(dataSourceMap.get(properties.getMaster())); |
52 | | - DynamicDSHolder.setDataSourceKey(properties.getMaster()); |
| 51 | + dynamicDs.setDefaultTargetDataSource(dataSourceMap.get(properties.getMaster())); |
| 52 | + DynamicDsHolder.setDataSourceKey(properties.getMaster()); |
53 | 53 | log.info("Create dynamic datasource over,set default to {}", properties.getMaster()); |
54 | | - return dynamicDS; |
| 54 | + return dynamicDs; |
55 | 55 | } |
56 | 56 |
|
57 | 57 | /** |
58 | 58 | * 检查是否配置了数据源和 master |
59 | 59 | */ |
60 | | - private void checkDynamicDSConfig() { |
| 60 | + private void checkDynamicDsConfig() { |
61 | 61 | // 检查是否配置了数据源 |
62 | 62 | if (Objects.isNull(properties.getDatasource()) || properties.getDatasource().isEmpty()) { |
63 | | - throw new DynamicDSException("Dynamic datasource config not found"); |
| 63 | + throw new DynamicDsException("Dynamic datasource config not found"); |
64 | 64 | } |
65 | 65 | // 检查是否指定主数据源 |
66 | 66 | if (Objects.isNull(properties.getMaster()) || !properties.getDatasource().containsKey(properties.getMaster())) { |
67 | | - throw new DynamicDSException("Not found default datasource config"); |
| 67 | + throw new DynamicDsException("Not found default datasource config"); |
68 | 68 | } |
69 | 69 | } |
70 | 70 | } |
0 commit comments