** 本文基于Spring 4.1.6 RELEASE源碼 **
先看看之前咱們在spring-mybatis.xml文件中配置的動態(tài)數(shù)據(jù)源昭躺,如下:
<bean id="dataSource" class="com.ricky.codelab.spring.ds.DynamicRoutingDataSource">
<property name="targetDataSources">
<map key-type="com.ricky.codelab.spring.ds.RouteStrategy">
<entry key="slave1" value-ref="slave1DataSource"/>
<entry key="slave2" value-ref="slave2DataSource"/>
</map>
</property>
<!-- 默認(rèn)目標(biāo)數(shù)據(jù)源為主庫數(shù)據(jù)源 -->
<property name="defaultTargetDataSource" ref="masterDataSource"/>
</bean>
DynamicRoutingDataSource 繼承自org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource
類领炫,代碼如下:
package com.ricky.codelab.spring.ds;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
/**
* 動態(tài)數(shù)據(jù)源切換
*
* @author Ricky Fung
* @create 2016-10-18 22:41
*/
public class DynamicRoutingDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
return DynamicRoutingContextHolder.getRouteStrategy();
}
}
AbstractRoutingDataSource 類的定義,源碼如下:
package org.springframework.jdbc.datasource.lookup;
public abstract class AbstractRoutingDataSource extends AbstractDataSource implements InitializingBean {
private Map<Object, Object> targetDataSources;
private Object defaultTargetDataSource;
private boolean lenientFallback = true;
private DataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
private Map<Object, DataSource> resolvedDataSources;
private DataSource resolvedDefaultDataSource;
}
AbstractRoutingDataSource 實現(xiàn)了InitializingBean接口似舵,Spring容器啟動時會回調(diào)其afterPropertiesSet()方法葱峡,AbstractRoutingDataSource afterPropertiesSet()如下:
public void afterPropertiesSet() {
if(this.targetDataSources == null) {
throw new IllegalArgumentException("Property \'targetDataSources\' is required");
} else {
this.resolvedDataSources = new HashMap(this.targetDataSources.size());
Iterator var1 = this.targetDataSources.entrySet().iterator();
while(var1.hasNext()) {
Entry entry = (Entry)var1.next();
Object lookupKey = this.resolveSpecifiedLookupKey(entry.getKey());
DataSource dataSource = this.resolveSpecifiedDataSource(entry.getValue());
this.resolvedDataSources.put(lookupKey, dataSource);
}
if(this.defaultTargetDataSource != null) {
this.resolvedDefaultDataSource = this.resolveSpecifiedDataSource(this.defaultTargetDataSource);
}
}
}
protected DataSource resolveSpecifiedDataSource(Object dataSource) throws IllegalArgumentException {
if(dataSource instanceof DataSource) {
return (DataSource)dataSource;
} else if(dataSource instanceof String) {
return this.dataSourceLookup.getDataSource((String)dataSource);
} else {
throw new IllegalArgumentException("Illegal data source value - only [javax.sql.DataSource] and String supported: " + dataSource);
}
}
這里主要把在xml中配置的targetDataSources解析到resolvedDataSources砰奕,defaultTargetDataSource 賦值到resolvedDefaultDataSource 提鸟。
另外仅淑,AbstractRoutingDataSource 繼承自AbstractDataSource,而AbstractDataSource 是DataSource的一個子類涯竟,如下:
package org.springframework.jdbc.datasource;
public abstract class AbstractDataSource implements DataSource {
protected final Log logger = LogFactory.getLog(this.getClass());
public AbstractDataSource() {
}
}
因此,我們只需要看AbstractRoutingDataSource 的getConnection()方法實現(xiàn)即可蝗蛙,如下:
public Connection getConnection() throws SQLException {
return this.determineTargetDataSource().getConnection();
}
public Connection getConnection(String username, String password) throws SQLException {
return this.determineTargetDataSource().getConnection(username, password);
}
determineTargetDataSource()方法如下:
protected DataSource determineTargetDataSource() {
Assert.notNull(this.resolvedDataSources, "DataSource router not initialized");
Object lookupKey = this.determineCurrentLookupKey();
DataSource dataSource = (DataSource)this.resolvedDataSources.get(lookupKey);
if(dataSource == null && (this.lenientFallback || lookupKey == null)) {
dataSource = this.resolvedDefaultDataSource;
}
if(dataSource == null) {
throw new IllegalStateException("Cannot determine target DataSource for lookup key [" + lookupKey + "]");
} else {
return dataSource;
}
}
determineTargetDataSource方法決定返回哪個DataSource 對象醉鳖,它根據(jù)determineCurrentLookupKey方法(這個方法正是我們在DynamicRoutingDataSource類中重寫的方法)獲取數(shù)據(jù)源的key哮内,然后從 resolvedDataSources中根據(jù)key取出DataSource ,如果返回的DataSource 為空纹因,則使用默認(rèn)的DataSource琳拨。