| | |
| | | // @Value("${dbUrl}") |
| | | private String dbUrl; |
| | | |
| | | private String dbName; |
| | | |
| | | @Bean(name = "hospDynamicDataSources") |
| | | public DataSource hospDynamicDataSources() { |
| | | HospDynamicDataSource dynamicDataSource = new HospDynamicDataSource(); |
| | | |
| | | FileInputStream fis = null; |
| | | Properties props = new Properties(); |
| | | try { |
| | | fis = new FileInputStream(url); |
| | | props.load(fis); |
| | | fis.close(); |
| | | dbUrl = props.getProperty("ip"); |
| | | dbName = props.getProperty("name"); |
| | | primaryPassword = props.getProperty("password"); |
| | | primaryPort = props.getProperty("prot"); |
| | | primaryUsername = props.getProperty("username"); |
| | | } catch (IOException e) { |
| | | throw new RuntimeException("读取配置文件失败", e); |
| | | } |
| | | String url = "jdbc:mysql://" + dbUrl + ":" + primaryPort + "/" + dbName; |
| | | // 初始化默认数据源为主库 |
| | | dynamicDataSource.addTargetDataSource(DEFAULT_DATA_SOURCE_KEY, createDataSource(primaryUrl, primaryUsername, primaryPassword)); |
| | | dynamicDataSource.addTargetDataSource(DEFAULT_DATA_SOURCE_KEY, createDataSource(url, primaryUsername, primaryPassword)); |
| | | |
| | | dynamicDataSource.setDefaultTargetDataSource(dynamicDataSource.getTargetDataSources().get(DEFAULT_DATA_SOURCE_KEY)); // 设置默认数据源 |
| | | return dynamicDataSource; |