Spring多数据源配置系列(一)——多数据源配置

来源:互联网 发布:foreach遍历一维数组 编辑:程序博客网 时间:2024/06/06 07:37

资源

Git地址:https://code.csdn.net/luo4105/study_multipledatasources

Spring多数据源

Spring中,可以通过AbstractRoutingDataSource来实现多数据源管理。这里步骤为

1. 在Spring注册多个数据源

2. 新建类继承AbstractRoutingDataSource,并配置

3. 给TheadLocal赋值

这种技术现在已经过时,现在大部分使用数据库代理。

Spring注册多个数据源

<bean id="cmsDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">    <property name="driverClassName" value="com.mysql.jdbc.Driver" />    <property name="url" value="jdbc:mysql://localhost:3306/aicms" />    <property name="username" value="root" />    <property name="password" value="123456" /></bean><bean id="epgDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">    <property name="driverClassName" value="com.mysql.jdbc.Driver" />    <property name="url" value="jdbc:mysql://localhost:3306/epgdb" />    <property name="username" value="root" />    <property name="password" value="123456" /></bean>

新建DynamicDataSource类继承AbstractRoutingDataSource,并配置

AbstractRoutingDataSource是spring的多数据源管理的抽象类,这里我们需要新建一个类继承它,并重写determineCurrentLookupKey方法,这个方法是多数据源的调用的逻辑部分,它返回一个多数据源的key,根据key找到对应的DataSource。在spring的配置DynamicDataSource时,需要指定targetDataSources,这就是目标数据源集,是一个map,key就是通过targetDataSources获得对应的数据源。这里我们使用TheadLocal是因为线程的安全。

public class DynamicDataSource extends AbstractRoutingDataSource {    public static ThreadLocal key = new ThreadLocal<String>();    @Override    protected Object determineCurrentLookupKey() {        return key.get();    }}

<bean id="dynamicDataSource" class="com.lc.rout.DynamicDataSource">    <property name="targetDataSources">        <map>            <entry key="cms" value-ref="cmsDataSource"/>            <entry key="epg" value-ref="epgDataSource"/>        </map>    </property></bean>

sqlSessionFactory的dataSource就是配置的dynamicDataSource

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">    <property name="dataSource" ref="dynamicDataSource" />    <property name="mapperLocations" value="mapper/*.xml" />    <property name="typeAliasesPackage" value="com.lc.model" /></bean>

给TheadLocal赋值

在Service层中,我们需要给key进行赋值,指定将使用的dataSource。实际就是在每个方法运行前给key赋值,在方法结束后移除赋值。

public Column selectOne(int id) {   DynamicDataSource.key.set("cms");   Column col =mapper.selectOne(id);   DataSourceKey.key.remove();   return col;}

当然,我们不可能像这样在每个方法前后加上赋值和移除复制的语句。这里可以用AOP实现,即自定义注解,注解参数是key。在service类上加上注解,调用service方法时,根据注解给key赋值。这里可以设置默认值,当service没有注解时,赋默认值。

这下面其实就是SpringAOP方面的知识了,便只放代码,不讲解了。

Spring配置开启AOP

<aop:aspectj-autoproxy proxy-target-class="true"/>

自定义注解

@Documented@Retention(RetentionPolicy.RUNTIME)@Target({ElementType.TYPE})public @interface DataSource {    String value() default EPG;    String CMS = "cms";    String EPG = "epg";}

切面逻辑

@Component@Aspectpublic class DataSourceAspect {    @Pointcut("execution(* com.lc.service.*.*(..))")    public void aspect() {    }    @Before("aspect()")    public void before(JoinPoint jp) throws Throwable{        Class<?> class1 = jp.getTarget().getClass();        DataSource anno = class1.getAnnotation(DataSource.class);        if(anno != null) {            DynamicDataSource.key.set(anno.value());        } else {            String key = (String) DataSource.class.getMethod("value").getDefaultValue();            DynamicDataSource.key.set(key);        }    }    @After("execution(* com.lc.service.*Service.*(..))")    public void after(JoinPoint jp) throws Throwable{        DynamicDataSource.key.remove();    }}

添加注解


原创粉丝点击