package com.yfd.platform.datasource; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.stereotype.Component; import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder; import javax.sql.DataSource; import java.util.HashMap; import java.util.Map; /****************************** * 用途说明: * 作者姓名: wxy * 创建时间: 2022/9/23 17:45 ******************************/ @Configuration @Component public class DynamicDataSourceConfig { @Bean @ConfigurationProperties("spring.datasource.druid.master") public DataSource wglMasterDataSource(){ return DruidDataSourceBuilder.create().build(); } @Bean @Primary public DynamicDataSource dataSource(DataSource wglMasterDataSource, DataSource wglSlaveDataSource) { Map targetDataSources = new HashMap<>(); targetDataSources.put("master",wglMasterDataSource); return new DynamicDataSource(wglMasterDataSource, targetDataSources); } }