beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<context:annotation-config />
<aop:aspectj-autoproxy />
<!--加入此段配置, -->
<context:component-scan base-package="zh.task" />
<!-- Enables the Spring Task @Scheduled programming model -->
<task:executor id="executor" pool-size="1" />
<task:scheduler id="scheduler" pool-size="1" />
<task:annotation-driven executor="executor"
scheduler="scheduler" />
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:jdbc.properties" />
</bean>
<!-- ========================= DataSource config ========================= -->
<bean id="dataSource1" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="url" value="${jdbc_url1}" />
<property name="username" value="${jdbc_user1}" />
<property name="password" value="${jdbc_password1}" />
<property name="maxActive" value="20" />
<property name="initialSize" value="1" />
<property name="maxWait" value="60000" />
<property name="minIdle" value="1" />
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<property name="minEvictableIdleTimeMillis" value="300000" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<property name="poolPreparedStatements" value="true" />
<property name="maxPoolPreparedStatementPerConnectionSize"
value="50" />
<property name="filters" value="stat,log4j" />
<property name="proxyFilters" ref="log-filter" />
</bean>
<bean id="dataSource2" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="url" value="${jdbc_url2}" />
<property name="username" value="${jdbc_user2}" />
<property name="password" value="${jdbc_password2}" />
<property name="maxActive" value="20" />
<property name="initialSize" value="1" />
<property name="maxWait" value="60000" />
<property name="minIdle" value="1" />
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<property name="minEvictableIdleTimeMillis" value="300000" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<property name="poolPreparedStatements" value="true" />
<property name="maxPoolPreparedStatementPerConnectionSize"
value="50" />
<property name="filters" value="stat,log4j" />
<property name="proxyFilters" ref="log-filter" />
</bean>
<bean id="log-filter" class="com.alibaba.druid.filter.logging.Log4jFilter">
<property name="resultSetLogEnabled" value="false" />
<property name="statementExecutableSqlLogEnable" value="true" />
<property name="statementLoggerName" value="sqlLogger" />
</bean>
<!-- ibatis配置 -->
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="classpath:ibatis.xml" />
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="dataSource" class="zh.dbcenter.DynamicDataSource1">
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry key="dataSource2" value-ref="dataSource2" />
<entry key="dataSource1" value-ref="dataSource1" />
</map>
</property>
<property name="defaultTargetDataSource" ref="dataSource1" />
</bean>
DbContextHolder.java
public class DbContextHolder {
private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();
public static void setDBType(String dbType) {
System.out.println((String) contextHolder.get());
contextHolder.set(dbType);
}
public static String getDBType() {
return (String) contextHolder.get();
}
public static void clearDBType() {
contextHolder.remove();
}
}
DynamicDataSource1.java
import org.apache.log4j.Logger;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class DynamicDataSource1 extends AbstractRoutingDataSource {
Logger log = Logger.getLogger("DynamicDataSource");
@Override
protected Object determineCurrentLookupKey() {
return DbContextHolder.getDBType();
}
}
DBCenter2.java
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import zh.Dao.CampusDao;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath*:beans1.xml"})
public class DBCenter2 {
@Autowired
private CampusDao campusDao;
@Test
public void testCampusAll() {
System.out.println("傻逼");
System.out.println(campusDao.findAll().get(0));
DbContextHolder.setDBType("dataSource2");
System.out.println(campusDao.findAll().get(0));
DbContextHolder.setDBType("default_dataSource");
System.out.println(campusDao.findAll().get(0));
}
}