Spring 配置多数据源解决方案

此文几处有引用别人博客内容,如有侵权表示歉意!

web项目中可能会遇到多数据源的情况,这个时候我们需要在不同的情况下使用不同的数据源对不同的数据库进行访问,最典型的案例是数据的读写分离。


多数据源的设计思路:
引用别人图片


多数据源的具体配置步骤:

  1. spring配置文件中,配置不同的数据源
<!-- 数据源1 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.user}"/>
        <property name="password" value="${jdbc.pwd}"/>
    </bean>
<!-- 数据源2 -->
    <bean id="dataSource2" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url2}"/>
        <property name="user" value="${jdbc.user}"/>
        <property name="password" value="${jdbc.pwd}"/>
    </bean>

2.实现DynamicDataSource类 并继承org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource类,重写determineCurrentLookupKey()方法:

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class DynamicDataSource extends AbstractRoutingDataSource {
    @Override
    protected Object determineCurrentLookupKey() {
        return DatabaseContextHolder.getCustomerType();
    }
}
  1. 实现DatabaseContextHolder类,内部实现ThreadLocal 类来保证线程安全
public class DatabaseContextHolder {
    private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();
    public static void setCustomerType(String customerType) {contextHolder.set(customerType);}
    public static String getCustomerType() {return contextHolder.get();}
    public static void clearCustomerType() {contextHolder.remove();}
}

4.配置数据源集合dynamicDataSource

<bean id="dynamicDataSource" class="com.laie.pa.common.dao.DynamicDataSource">  
        <property name="targetDataSources">  
            <map key-type="java.lang.String">  
                <entry value-ref="dataSource" key="dataSource"></entry>  
                <entry value-ref="dataSource2" key="dataSource2"></entry>  
            </map>  
        </property>
       <!—配置默认数据源 -->  
        <property name="defaultTargetDataSource" ref="dataSource"></property>  
    </bean>
  1. 配置数据源切换
    5.1 实现DataSourceInterceptor 类并配置Bean
import org.aspectj.lang.JoinPoint;
import org.springframework.stereotype.Component;

@Component 
public class DataSourceInterceptor {
    public void setdataSourceOne(JoinPoint jp) {
        DatabaseContextHolder.setCustomerType("dataSource");
    }
    public void setdataSourceTwo(JoinPoint jp) {
        DatabaseContextHolder.setCustomerType("dataSource2");
    }
}

Spring.xml 文件配置此Bean

<bean id="dataSourceInterceptor" class="com.laie.pa.common.dao.DataSourceInterceptor" />

5.2 配置AOP 实现数据源切换

<aop:config>  
        <aop:aspect id="dataSourceAspect" ref="dataSourceInterceptor">  
            <aop:pointcut id="daoOne" expression="execution(* com.laie.pa.action.*.*(..))" />  
            <aop:pointcut id="daoTwo" expression="execution(* com.laie.pa.action2.*.*(..))" />  
            <aop:before pointcut-ref="daoOne" method="setdataSourceOne" />  
            <aop:before pointcut-ref="daoTwo" method="setdataSourceTwo" />  
        </aop:aspect>  
    </aop:config>   

注:实现切换利用了aop中配置了执行的包下的所有方法使用不同的数据源。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT从业者的职业生涯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值