All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gitee.huanminabc.utils_tools.dynamic_datasource.bean.DataSourceInitialize Maven / Gradle / Ivy

There is a newer version: 1.0.6-RELEASE
Show newest version
package com.gitee.huanminabc.utils_tools.dynamic_datasource.bean;


import com.gitee.huanminabc.utils_common.base.UniversalException;

import com.gitee.huanminabc.utils_tools.dynamic_datasource.aop.DBSwitch;
import com.gitee.huanminabc.utils_tools.dynamic_datasource.base.DynamicDataSourceService;
import com.gitee.huanminabc.utils_tools.dynamic_datasource.dal.mapper.DatasourceMapper;
import com.gitee.huanminabc.utils_tools.dynamic_datasource.entity.DataSourceEneity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.util.List;

//从数据库中查询出全部的数据源,添加到数据源容器中

/**
 * 表结构初始化文件: resources/sql/init.sql
 */

//依赖于DynamicDataSourceConfig中的abstractRoutingDataSource
@DependsOn("abstractRoutingDataSource")
@Component
@Slf4j
@DBSwitch("resource")
public class DataSourceInitialize implements ApplicationRunner  {

    @Autowired
    private DatasourceMapper datasourceMapper;

    @Value("${spring.datasource.database-load-activate}")
    private Boolean databaseLoadActivate;

    //项目启动后执行初始化数据源
    @Override
    public void run(ApplicationArguments args) throws Exception {
        if (databaseLoadActivate){
            log.info("-----------------DataSourceInitialize------------------");
            try {
                List dataSources = datasourceMapper.getDataSources();
                for (DataSourceEneity dataSource : dataSources) {

                    if (DynamicDataSourceService.containsDataSource(dataSource.getKey())) {
                        log.error("[DataSourceInitialize->run] 注册的数据源已存在 key:{}", dataSource.getKey());
                        System.exit(1);
                    }
                    DataSourceProperties dataSourceProperties = new DataSourceProperties();
                    dataSourceProperties.setUrl(dataSource.getUrl());
                    dataSourceProperties.setUsername(dataSource.getUsername());
                    dataSourceProperties.setPassword(dataSource.getPassword());
                    dataSourceProperties.setDriverClassName( dataSource.getDriverClassName());
                    dataSourceProperties.setType((Class) Class.forName(dataSource.getPool()));
                    DynamicDataSourceService.addDataSource(dataSource.getKey(),dataSource.getDataSource(),dataSourceProperties);
                }
            } catch (Exception e) {
                UniversalException.logError(e);
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy