• springboot启动中动态修改数据源配置


    package com.hqhp.salesys.common.config;
    
    import cn.hutool.core.io.file.FileReader;
    import com.baomidou.dynamic.datasource.provider.AbstractDataSourceProvider;
    import com.baomidou.dynamic.datasource.provider.DynamicDataSourceProvider;
    import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
    import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties;
    import com.hqhp.salesys.common.constant.HqhpConstant;
    import com.hqhp.salesys.common.properties.HqhpProperties;
    import com.hqhp.salesys.common.utils.DesDelphi;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    
    import javax.sql.DataSource;
    import java.io.File;
    import java.util.Map;
    
    /**
    * @author xucj
    * @date 2022/08/11/16:37
    * 

    * 自定义yml文件动态数据源解析类 * 处理数据库密码从指定文件获取密码字段并解密设置到上下文中 */ @Component public class MyYmlDynamicDataSourceProvider extends AbstractDataSourceProvider implements DynamicDataSourceProvider { @Autowired private HqhpProperties hqhpProperties; private DynamicDataSourceProperties properties; public MyYmlDynamicDataSourceProvider(DynamicDataSourceProperties properties) { this.properties = properties; } @Override public Map loadDataSources() { Map dataSourcePropertiesMap = this.properties.getDatasource(); DataSourceProperty dataSourceProperty = dataSourcePropertiesMap.get(HqhpConstant.MASTER); if(dataSourceProperty != null){ File file = new File(hqhpProperties.getCretPath() + HqhpConstant.DB_FILE); if(file.isFile()){ FileReader fileReader = FileReader.create(file); dataSourceProperty.setPassword(DesDelphi.decrypt(fileReader.readString(), HqhpConstant.DB_SECRET_KEY)); } } return this.createDataSourceMap(dataSourcePropertiesMap); } }

    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
  • 相关阅读:
    spring cache (Redis方式)
    VoLTE端到端业务详解 | 传统移动通信网络
    SpringBoot一站式功能提供框架(一)--柚子真好吃
    Kubernetes(K8s):未来云原生应用的引擎
    day33 List接口
    3环境变量
    98 验证二叉搜索树
    【DeepLearning 8】Self-Attention自注意力神经网络
    阿里新零售中的智能补货(I)— 库存模型
    前端项目使用钉钉字体
  • 原文地址:https://blog.csdn.net/qq_15076569/article/details/128145663