javascript
Spring+Mybatis多数据源配置(四)——AbstractRoutingDataSource实现数据源动态切换
歡迎跳轉(zhuǎn)到本文的原文鏈接:https://honeypps.com/java/spring-mybatis-multi-datasource-props-4/
有時(shí)候需要在程序中動(dòng)態(tài)切換數(shù)據(jù)源,那么這個(gè)系列的之前的博文所闡述的方法就不再使用了,總不能通過程序更改config.properties文件的dataSource的值,然后再重啟web服務(wù)器以便加載applicationContext.xml文件。這里講訴的是如何利用AbstractRoutingDataSource進(jìn)行數(shù)據(jù)源動(dòng)態(tài)切換。
首先上applicationContext.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:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beansclasspath:/org/springframework/beans/factory/xml/spring-beans-3.0.xsdhttp://www.springframework.org/schema/aop classpath:/org/springframework/aop/config/spring-aop-3.0.xsdhttp://www.springframework.org/schema/contextclasspath:/org/springframework/context/config/spring-context-3.0.xsdhttp://www.springframework.org/schema/tx classpath:/org/springframework/transaction/config/spring-tx-3.0.xsd"><!-- IoC配置 --><!-- 掃描類包,將標(biāo)注Spring注解的類自動(dòng)轉(zhuǎn)化Bean,同時(shí)完成Bean的注入 --><context:component-scan base-package="com.shr.dao" /><context:component-scan base-package="com.shr.service" /><!-- DAO配置 --><context:property-placeholder location="classpath:config.properties"/><bean id="mysql" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"><property name="driverClassName" value="${mysql_driver}"/><property name="url" value="${mysql_url}"/><property name="username" value="${mysql_username}"/><property name="password" value="${mysql_password}"/></bean><bean id="oracle" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"><property name="driverClassName" value="${ora_driver}"/><property name="url" value="${ora_url}"/><property name="username" value="${ora_username}"/><property name="password" value="${ora_password}"/></bean><bean id="dataSource" class="com.shr.dao.datasource.DataSources"><property name="targetDataSources"><map key-type="java.lang.String"><entry value-ref="mysql" key="MYSQL"></entry><entry value-ref="oracle" key="ORACLE"></entry></map></property><property name="defaultTargetDataSource" ref="mysql"></property></bean><bean id="vendorProperties"class="org.springframework.beans.factory.config.PropertiesFactoryBean"><property name="properties"><props><prop key="Oracle">oracle</prop><prop key="MySQL">mysql</prop></props></property></bean><bean id="databaseIdProvider" class="org.apache.ibatis.mapping.VendorDatabaseIdProvider"><property name="properties" ref="vendorProperties" /></bean><bean name="myBatisSQLInterceptor" class="com.shr.dao.MyBatisSQLInterceptor"></bean><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource" /><property name="typeAliasesPackage" value="com.shr.dao.pojo,com.shr.dao.model" /><property name="databaseIdProvider" ref="databaseIdProvider" /><property name="mapperLocations"><list><value>classpath:com/shr/dao/resources/mappers/*_mapper.xml</value></list></property><!-- <property name="configLocation" value="/WEB-INF/mybatis-config.xml"/> --><property name="typeHandlersPackage" value="com.shr.dao" /><property name="plugins"><list><ref bean="myBatisSQLInterceptor"/></list></property></bean><!-- 配置事務(wù)管理器 --><tx:annotation-driven/><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="${dataSource}"/></bean><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="com.shr.dao.mapper"/><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> <!-- <property name="markerInterface" value="com.shr.dao.mapper.ITemplateMapper"/> --></bean> </beans>我們可以觀察到文件中多了一段:
?
?
<bean id="dataSource" class="com.shr.dao.datasource.DataSources"><property name="targetDataSources"><map key-type="java.lang.String"><entry value-ref="mysql" key="MYSQL"></entry><entry value-ref="oracle" key="ORACLE"></entry></map></property><property name="defaultTargetDataSource" ref="mysql"></property></bean>而且sqlSessionFactory的dataSource是關(guān)聯(lián)到上面這段內(nèi)容的,而不是通過${dataSource}讀取config.properties文件的內(nèi)容獲取的。
?
這個(gè)com.shr.dao.datasource.DataSources是自定義的類,繼承自AbstractRoutingDataSource類,實(shí)現(xiàn)其determineCurrentLookupKey()方法。
代碼如下:
?
package com.shr.dao.datasource; import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; public class DataSources extends AbstractRoutingDataSource {@Overrideprotected Object determineCurrentLookupKey(){return DataSourceSwitch.getDataSourceType();} } package com.shr.dao.datasource;public class DataSourceSwitch {private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();public static void setDataSourceType(String dataSourceType){contextHolder.set(dataSourceType);}public static String getDataSourceType(){return contextHolder.get();}public static void clearDataSourceType(){contextHolder.remove();} } package com.shr.dao.datasource;public class DataSourceInstances {public static final String MYSQL="MYSQL";public static final String ORACLE="ORACLE"; }同樣,我們通過一個(gè)junit測(cè)試用例進(jìn)行驗(yàn)證:
?
?
package com.shr.dao.datasource;import java.util.List;import javax.inject.Inject;import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.transaction.TransactionConfiguration; import org.springframework.transaction.annotation.Transactional;import com.shr.dao.datasource.DataSourceInstances; import com.shr.dao.datasource.DataSourceSwitch; import com.shr.dao.model.userManage.UserListInfo; import com.shr.service.userManage.UserManageService;@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("file:WebContent/WEB-INF/applicationContext.xml") @Transactional @TransactionConfiguration(transactionManager="transactionManager",defaultRollback=false) public class DynamicDataSourceTest {@Injectprivate UserManageService userManageService;@Testpublic void test(){DataSourceSwitch.setDataSourceType(DataSourceInstances.MYSQL);List<UserListInfo> list = userManageService.getUserListInfo();for(UserListInfo user : list){System.out.println(user.getUser_name());}} }通過改變DataSourceSwitch.setDataSourceType(DataSourceInstances.ORACLE);可以轉(zhuǎn)換不同的數(shù)據(jù)源.
歡迎跳轉(zhuǎn)到本文的原文鏈接:https://honeypps.com/java/spring-mybatis-multi-datasource-props-4/
總結(jié)
以上是生活随笔為你收集整理的Spring+Mybatis多数据源配置(四)——AbstractRoutingDataSource实现数据源动态切换的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring+Mybatis多数据源配置
- 下一篇: JAVA通信编程(一)——串口通讯