Spring动态数据源的配置及使用

    如果应用系统使用到多个数据源,可以使用Spring提供的动态数据源进行配置:

1.DynamicDataSource的设计

通过继承AbstractRoutingDataSource实现动态数据源的设计

public class DynamicDataSource extends AbstractRoutingDataSource {

    @Override
    protected Object determineCurrentLookupKey() {

        return DataSourceContextHolder.getDataSourceType();
    }

}

 DataSourceContextHolder类的设计:

public class DataSourceContextHolder {
    private static final ThreadLocal contextHolder = new ThreadLocal(); // 线程本地环境

    // 设置数据源类型
    public static void setDataSourceType(String dataSourceType) {
        contextHolder.set(dataSourceType);
    }

    // 获取数据源类型   
    public static String getDataSourceType() {
        return (String) contextHolder.get();
    }
    // 清除数据源类型

    public static void clearDataSourceType() {
        contextHolder.remove();
    }
}

 2.Spring的配置

假设有两个数据源framework及stk,

<?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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
	http://www.springframework.org/schema/tx 
	http://www.springframework.org/schema/tx/spring-tx.xsd
	http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.1.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.1.xsd" 
	>
    <context:component-scan base-package="com.framework.dao"/>
    <context:component-scan base-package="com.framework.dao.mssql"/>
    <context:component-scan base-package="com.framework.service"/>
    <context:component-scan base-package="com.framework.action"/>

    <context:component-scan base-package="com.stk.dao.mssql"/>
    <context:component-scan base-package="com.stk.service"/>
    <context:component-scan base-package="com.stk.action"/>

    
    <bean id="propertyConfigurerFW"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:framework.properties"/>
    </bean>

	<bean id="datasourceFW"
		class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close" 
		p:driverClassName="${jdbc.driverClassName}"
		p:url="${jdbc.url}" 
		p:username="${jdbc.username}" 
		p:password="${jdbc.password}" />

    <bean id="datasourceStk"
          class="org.apache.commons.dbcp.BasicDataSource"
          destroy-method="close"
          p:driverClassName="${jdbcstk.driverClassName}"
          p:url="${jdbcstk.url}"
          p:username="${jdbcstk.username}"
          p:password="${jdbcstk.password}" />


    <!-- 编写spring 配置文件的配置多数源映射关系 -->
    <bean class="com.framework.dao.DynamicDataSource"
          id="dataSource">
        <property name="targetDataSources">
            <map key-type="java.lang.String">
                <entry value-ref="datasourceFW" key="framework"></entry>
                <entry value-ref="datasourceStk" key="stk"></entry>
            </map>
        </property>
        <property name="defaultTargetDataSource"
                  ref="datasourceFW">
        </property>
    </bean>



    <bean id="sessionFactory"
          class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!--  -->
        <property name="packagesToScan" value="com.framework.domain,com.stk.domain"/>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">com.util.SQLServerDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <!--  3.1
                <prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</prop>
                <prop key="hibernate.cache.use_second_level_cache">true</prop>
                <prop key="hibernate.cache.use_query_cache">true</prop>
               -->
                <!--  4.0-->
                <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
                <prop key="hibernate.cache.use_second_level_cache">true</prop>

            </props>
        </property>
    </bean>

    <bean id="hibernateTemplate"
          class="org.springframework.orm.hibernate4.HibernateTemplate"
          p:sessionFactory-ref="sessionFactory" />
   </beans>

3.DAO的使用

DataSourceContextHolder.setDataSourceType("framework");

frameworkDAO.操作()

DataSourceContextHolder.setDataSourceType("stk");

stkDAO.操作()

相关推荐