本文整理匯總了Java中org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource類的典型用法代碼示例。如果您正苦於以下問題:Java AbstractRoutingDataSource類的具體用法?Java AbstractRoutingDataSource怎麽用?Java AbstractRoutingDataSource使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
AbstractRoutingDataSource類屬於org.springframework.jdbc.datasource.lookup包,在下文中一共展示了AbstractRoutingDataSource類的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: roundRobinDataSouceProxy
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; //導入依賴的package包/類
/**
* 有多少個數據源就要配置多少個bean
* @return
*/
@Bean
public AbstractRoutingDataSource roundRobinDataSouceProxy() {
int size = Integer.parseInt(dataSourceSize);
DynamicDataSource proxy = new DynamicDataSource(size);
Map<Object, Object> targetDataSources = new HashMap<>();
// 寫
targetDataSources.put(DataSourceType.write.getType(),dataSource);
//多個讀數據庫時
for (int i = 0; i < size; i++) {
targetDataSources.put(i, readDataSources.get(i));
}
proxy.setDefaultTargetDataSource(dataSource);
proxy.setTargetDataSources(targetDataSources);
return proxy;
}
示例2: roundRobinDataSouceProxy
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; //導入依賴的package包/類
/**
* 有多少個數據源就要配置多少個bean
* @return
*/
@Bean
public AbstractRoutingDataSource roundRobinDataSouceProxy() {
int size = Integer.parseInt(dataSourceSize);
MyAbstractRoutingDataSource proxy = new MyAbstractRoutingDataSource(size);
Map<Object, Object> targetDataSources = new HashMap<Object, Object>();
// DataSource writeDataSource = SpringContextHolder.getBean("writeDataSource");
// 寫
targetDataSources.put(DataSourceEnums.write.getType(),dataSource);
// targetDataSources.put(DataSourceType.read.getType(),readDataSource);
//多個讀數據庫時
for (int i = 0; i < size; i++) {
targetDataSources.put(i, readDataSources.get(i));
}
proxy.setDefaultTargetDataSource(dataSource);
proxy.setTargetDataSources(targetDataSources);
return proxy;
}
示例3: roundRobinDataSouceProxy
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; //導入依賴的package包/類
/**
* 有多少個數據源就要配置多少個bean
* @return
*/
@Bean
public AbstractRoutingDataSource roundRobinDataSouceProxy() {
int size = Integer.parseInt(dataSourceSize);
MyAbstractRoutingDataSource proxy = new MyAbstractRoutingDataSource(size);
Map<Object, Object> targetDataSources = new HashMap<Object, Object>();
// DataSource writeDataSource = SpringContextHolder.getBean("writeDataSource");
//設置寫庫
targetDataSources.put(DataSourceType.write.getType(),dataSource);
// targetDataSources.put(DataSourceType.read.getType(),readDataSource);
//設置多讀庫
for (int i = 0; i < size; i++) {
targetDataSources.put(i, readDataSources.get(i));
}
proxy.setDefaultTargetDataSource(dataSource);
proxy.setTargetDataSources(targetDataSources);
return proxy;
}
示例4: routingDataSource
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; //導入依賴的package包/類
/**
* 注入AbstractRoutingDataSource
* @param writeDataSource
* @param read1DataSource
* @param read2DataSource
* @return
* @throws Exception
*/
@Bean
public AbstractRoutingDataSource routingDataSource(
@Qualifier("writeDataSource") DataSource writeDataSource,
@Qualifier("read1DataSource") DataSource read1DataSource,
@Qualifier("read2DataSource") DataSource read2DataSource
) throws Exception {
DynamicDataSource dataSource = new DynamicDataSource();
Map<Object, Object> targetDataSources = new HashMap();
targetDataSources.put(WRITE_DATASOURCE_KEY, writeDataSource);
targetDataSources.put(READ1_DATASOURCE_KEY, read1DataSource);
targetDataSources.put(READ2_DATASOURCE_KEY, read2DataSource);
dataSource.setTargetDataSources(targetDataSources);
dataSource.setDefaultTargetDataSource(writeDataSource);
return dataSource;
}
示例5: sqlSessionFactory
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; //導入依賴的package包/類
/**
* 根據數據源創建SqlSessionFactory
*/
@Bean
public SqlSessionFactory sqlSessionFactory(AbstractRoutingDataSource routingDataSource) throws Exception {
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(routingDataSource);// 指定數據源(這個必須有,否則報錯)
// 下邊兩句僅僅用於*.xml文件,如果整個持久層操作不需要使用到xml文件的話(隻用注解就可以搞定),則不加
factoryBean.setTypeAliasesPackage("com.tangcheng.datasources.aop.model");// 指定基包
factoryBean.setMapperLocations(resolver.getResources("classpath:mapper/**/*.xml"));//
return factoryBean.getObject();
}
示例6: afterPropertiesSet
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; //導入依賴的package包/類
@SuppressWarnings("unchecked")
@Override
public void afterPropertiesSet() {
super.afterPropertiesSet();
// 由於父類的resolvedDataSources屬性是私有的子類獲取不到,需要使用反射獲取
Field field = ReflectionUtils.findField(AbstractRoutingDataSource.class, "resolvedDataSources");
field.setAccessible(true); // 設置可訪問
try {
Map<Object, DataSource> resolvedDataSources = (Map<Object, DataSource>) field.get(this);
// 讀庫的數據量等於數據源總數減去寫庫的數量
this.slaveCount = resolvedDataSources.size() - 1;
for (Map.Entry<Object, DataSource> entry : resolvedDataSources.entrySet()) {
if (DynamicDataSourceHolder.MASTER.equals(entry.getKey())) {
continue;
}
slaveDataSources.add(entry.getKey());
}
} catch (Exception e) {
LOGGER.error("afterPropertiesSet error! ", e);
}
}
示例7: roundRobinDataSouceProxy
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; //導入依賴的package包/類
/**
* 有多少個數據源就要配置多少個bean
* @return
*/
@Bean(name = "roundRobinDataSouceProxy")
public AbstractRoutingDataSource roundRobinDataSouceProxy() {
int size = Integer.parseInt(dataSourceSize);
MyAbstractRoutingDataSource proxy = new MyAbstractRoutingDataSource(size);
Map<Object, Object> targetDataSources = new HashMap<Object, Object>();
DataSource writeDataSource = SpringContextHolder.getBean("writeDataSource");
// 寫
targetDataSources.put(DataSourceType.write.getType(), SpringContextHolder.getBean("writeDataSource"));
for (int i = 0; i < size; i++) {
targetDataSources.put(i, SpringContextHolder.getBean("readDataSource" + (i + 1)));
}
proxy.setDefaultTargetDataSource(writeDataSource);
proxy.setTargetDataSources(targetDataSources);
return proxy;
}
示例8: getTargetDataSources
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; //導入依賴的package包/類
@SuppressWarnings("unchecked")
public static Map<Object, Object> getTargetDataSources() {
try {
Field field = AbstractRoutingDataSource.class
.getDeclaredField("targetDataSources");
field.setAccessible(true);
return (Map<Object, Object>) field.get(SpringContextUtils
.getBean("dataSource"));
} catch (NoSuchFieldException | SecurityException
| IllegalArgumentException | IllegalAccessException e) {
e.printStackTrace();
return null;
}
}
示例9: routingDataSource
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; //導入依賴的package包/類
/**
* 注入AbstractRoutingDataSource
* @param readDruidDataSource
* @param writeDruidDataSource
* @return
* @throws Exception
*/
@Bean
public AbstractRoutingDataSource routingDataSource(
@Qualifier(READ_DATASOURCE_KEY) DataSource readDruidDataSource,
@Qualifier(WRITE_DATASOURCE_KEY) DataSource writeDruidDataSource
) throws Exception {
DynamicDataSource dataSource = new DynamicDataSource();
Map<Object, Object> targetDataSources = new HashMap();
targetDataSources.put(WRITE_DATASOURCE_KEY, writeDruidDataSource);
targetDataSources.put(READ_DATASOURCE_KEY, readDruidDataSource);
dataSource.setTargetDataSources(targetDataSources);
dataSource.setDefaultTargetDataSource(writeDruidDataSource);
return dataSource;
}
示例10: transactionManager
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; //導入依賴的package包/類
@Bean
public PlatformTransactionManager transactionManager(AbstractRoutingDataSource dataSource) throws Exception {
return new DataSourceTransactionManager(dataSource);
}
示例11: roundRobinDataSouceProxy
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; //導入依賴的package包/類
/**
* See {@literal http://blog.csdn.net/dream_broken/article/details/72851329}
*/
@Bean(name="roundRobinDataSouceProxy")
public AbstractRoutingDataSource roundRobinDataSouceProxy() {
Map<Object, Object> targetDataSources = new HashMap<>();
//把所有數據庫都放在targetDataSources中,注意key值要和determineCurrentLookupKey()中代碼寫的一至,
//否則切換數據源時找不到正確的數據源
targetDataSources.put(DataSourceContextHolder.DataSourceType.write.getType(), writeDataSource);
targetDataSources.put(DataSourceContextHolder.DataSourceType.read.getType()+"1", readDataSource01);
final int readSize = Integer.parseInt(readDataSourceSize);
//路由類,尋找對應的數據源
AbstractRoutingDataSource proxy = new AbstractRoutingDataSource(){
private AtomicInteger count = new AtomicInteger(0);
/**
* 這是AbstractRoutingDataSource類中的一個抽象方法,
* 而它的返回值是你所要用的數據源dataSource的key值,有了這個key值,
* targetDataSources就從中取出對應的DataSource,如果找不到,就用配置默認的數據源。
*/
@Override
protected Object determineCurrentLookupKey() {
String typeKey = DataSourceContextHolder.getReadOrWrite();
if(typeKey == null){
throw new NullPointerException("數據庫路由時,決定使用哪個數據庫源類型不能為空...");
}
if (typeKey.equals(DataSourceContextHolder.DataSourceType.write.getType())){
return DataSourceContextHolder.DataSourceType.write.getType();
}
//讀庫, 簡單負載均衡
int number = count.getAndAdd(1);
int lookupKey = number % readSize;
return DataSourceContextHolder.DataSourceType.read.getType()+(lookupKey+1);
}
};
proxy.setDefaultTargetDataSource(writeDataSource);//默認庫
proxy.setTargetDataSources(targetDataSources);
return proxy;
}