Class DynamicDataSource

java.lang.Object
org.springframework.jdbc.datasource.AbstractDataSource
org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource
org.aoju.bus.starter.jdbc.DynamicDataSource
All Implemented Interfaces:
Wrapper, CommonDataSource, DataSource, org.springframework.beans.factory.InitializingBean

public class DynamicDataSource extends org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource
多数据源支持
Since:
Java 17+
Version:
6.5.0
Author:
Kimi Liu
  • Constructor Details

    • DynamicDataSource

      public DynamicDataSource()
  • Method Details

    • getInstance

      public static DynamicDataSource getInstance()
      单例方法
      Returns:
      the DynamicDataSource
    • addDataSource

      public static void addDataSource(String key, DataSource dataSource)
      动态增加数据源
      Parameters:
      key - 数据源key
      dataSource - 数据源信息
    • setTargetDataSources

      public void setTargetDataSources(Map<Object,Object> map)
      Overrides:
      setTargetDataSources in class org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource
    • determineCurrentLookupKey

      protected Object determineCurrentLookupKey()
      AbstractRoutingDataSource 抽象类实现方法, 即获取当前线程数据源的key
      Specified by:
      determineCurrentLookupKey in class org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource
      Returns:
      当前数据源key
    • afterPropertiesSet

      public void afterPropertiesSet()
      在获取key的集合,目的只是为了添加一些告警日志
      Specified by:
      afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean
      Overrides:
      afterPropertiesSet in class org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource
    • containsKey

      public boolean containsKey(String key)
      判断指定DataSrouce当前是否存在
      Parameters:
      key - 数据源key
      Returns:
      the true/false
    • getDefaultDataSource

      public DataSource getDefaultDataSource()
      获取默认数据源
      Returns:
      the dataSource