Java注解--实现动态数据源切换

摘要:
从而在运行时实现多个数据源的动态切换。}抽象方法determineCurrentLookupKey()返回DataSource的键值,然后根据键从resolvedDataSources映射中获取相应的DataSource。我们需要做的是实现抽象方法determineCurrentLookupKey()以返回数据源的键值。

当一个项目中有多个数据源(也可以是主从库)的时候,我们可以利用注解在mapper接口上标注数据源,从而来实现多个数据源在运行时的动态切换。

实现原理

在Spring 2.0.1中引入了AbstractRoutingDataSource, 该类充当了DataSource的路由中介, 能有在运行时, 根据某种key值来动态切换到真正的DataSource上。

看下AbstractRoutingDataSource:

public abstract class AbstractRoutingDataSource extends AbstractDataSource implements InitializingBean

AbstractRoutingDataSource继承了AbstractDataSource,获取数据源部分:

/** 
 * Retrieve the current target DataSource. Determines the 
 * {@link #determineCurrentLookupKey() current lookup key}, performs 
 * a lookup in the {@link #setTargetDataSources targetDataSources} map, 
 * falls back to the specified 
 * {@link #setDefaultTargetDataSource default target DataSource} if necessary. 
 * @see #determineCurrentLookupKey() 
 */  
protected DataSource determineTargetDataSource() {  
    Assert.notNull(this.resolvedDataSources, "DataSource router not initialized");  
    Object lookupKey = determineCurrentLookupKey();  
    DataSource dataSource = this.resolvedDataSources.get(lookupKey);  
    if (dataSource == null && (this.lenientFallback || lookupKey == null)) {  
        dataSource = this.resolvedDefaultDataSource;  
    }  
    if (dataSource == null) {  
        throw new IllegalStateException("Cannot determine target DataSource for lookup key [" + lookupKey + "]");  
    }  
    return dataSource;  
}

抽象方法determineCurrentLookupKey()返回DataSource的key值,然后根据这个key从resolvedDataSources这个map里取出对应的DataSource,如果找不到,则用默认的resolvedDefaultDataSource。

我们要做的就是实现抽象方法determineCurrentLookupKey()返回数据源的key值。

使用方法

定义注解:

/**
 * Created by huangyangquan on 2016/11/30.
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface DataSource {

    DataSourceType value();

}

注解为数据源的名称,可定义一个枚举类表示:

/**
 * Created by huangyangquan on 2016/11/30.
 */
public enum DataSourceType {

    MASTER,
    SLAVE

}

注解定义好了,我们利用Spring的AOP根据注解内容对数据源进行选择,这里需要利用上面提到的AbstractRoutingDataSource类,该类是能够实现数据源切换的关键所在。

定义类DynamicDataSource继承AbstractRoutingDataSource,并实现determineCurrentLookupKey(),返回数据源的key值。

/**
 * Created by huangyangquan on 2016/11/30.
 */
public class DynamicDataSource extends AbstractRoutingDataSource {

    @Override
    protected Object determineCurrentLookupKey() {
        return DynamicDataSourceHolder.getDataSourceType();
    }

}

DynamicDataSourceHolder是我们管理DataSource的类,将一次数据库操作的数据源名称保存在DynamicDataSourceHolder中,以供后面的操作在此context中取数据源key,其中DataSourceType使用了线程本地变量来保证线程安全。

/**
 * Created by huangyangquan on 2016/11/30.
 */
public class DynamicDataSourceHolder {

    // 线程本地环境
    private static final ThreadLocal<DataSourceType> contextHolder = new ThreadLocal<DataSourceType>();

    // 设置数据源类型
    public static void setDataSourceType(DataSourceType dataSourceType) {
        Assert.notNull(dataSourceType, "DataSourceType cannot be null");
        contextHolder.set(dataSourceType);
    }

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

    // 清除数据源类型
    public static void clearDataSourceType() {
        contextHolder.remove();
    }

}

我们在Spring的配置文件中配置数据源key值得对应关系:

<bean   class="com.aheizi.config.DynamicDataSource">
    <property name="targetDataSources">
        <map key-type="java.lang.String">
            <entry key="MASTER" value-ref="TEST-MASTER-DB"></entry>
            <entry key="SLAVE" value-ref="TEST-SLAVE-DB"></entry>
        </map>
    </property>
    <property name="defaultTargetDataSource" ref="TEST-MASTER-DB">
    </property>
</bean>

设置targetDataSources和defaultTargetDataSource。TEST-MASTER-DBTEST-SLAVE-DB表示主库的从库,是我们的两个数据源。

接下来配置AOP切面:

<aop:aspectj-autoproxy proxy-target-  />
<bean     />
<aop:config>
    <aop:aspect   ref="manyDataSourceAspect">
        <aop:pointcut expression="execution(* com.aheizi.dao.*.*(..))"
              /><!-- 配置切点 -->
        <aop:before pointcut-ref="dataSourceCutPoint" method="before" />
    </aop:aspect>
</aop:config>

以下是切面中before执行的DataSourceAspect的实现,主要实现的功能是获取方法上的注解,根据注解名称将值设置到DynamicDataSourceHolder中,这样在执行查询的时候,determineCurrentLookupKey()返回数据源的key值就是我们希望的那个数据源了。

/**
 * Created by huangyangquan on 2016/11/30.
 */
public class DataSourceAspect {

    private static final Logger LOG = LoggerFactory.getLogger(DataSourceAspect.class);

    public void before(JoinPoint point){
        Object target = point.getTarget();
        String method = point.getSignature().getName();
        Class<?>[] classz = target.getClass().getInterfaces();
        Class<?>[] parameterTypes = ((MethodSignature) point.getSignature()).getMethod().getParameterTypes();
        try {
            Method m = classz[0].getMethod(method, parameterTypes);
            if (m != null && m.isAnnotationPresent(DataSource.class)) {
                // 访问mapper中的注解
                DataSource data = m.getAnnotation(DataSource.class);
                switch (data.value()) {
                    case MASTER:
                        DynamicDataSourceHolder.setDataSourceType(DataSourceType.MASTER);
                        LOG.info("using dataSource:{}", DataSourceType.MASTER);
                        break;
                    case SLAVE:
                        DynamicDataSourceHolder.setDataSourceType(DataSourceType.SLAVE);
                        LOG.info("using dataSource:{}", DataSourceType.SLAVE);
                        break;
                }
            }
        } catch (Exception e) {
            LOG.error("dataSource annotation error:{}", e.getMessage());
            // 若出现异常,手动设为主库
            DynamicDataSourceHolder.setDataSourceType(DataSourceType.MASTER);
        }
    }

}

这样我们就实现了一个动态数据源切换的功能。

免责声明:文章转载自《Java注解--实现动态数据源切换》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇无线路由器无缝漫游【Unity/Kinect】手势识别Gesture下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

解决JS文件页面加载时的阻塞

关于页面加载时的时间消费,许多书中都做出了介绍,也提出了很多种方法。本文章就详细介绍XHR注入。 概述:JS分拆的方法 1.XHR注入:就是用ajax异步请求同域包含脚本的文件,然后将返回的字符串转化为脚本使用,该方法不会造成页面渲染和onload事件的阻塞,因为是异步处理,推荐使用。 2.iframe注入:加载一个iframe框架,通过使用iframe框...

智能指针的实现(指针运算符重载)

智能指针的实现 Person类有showAge 成员函数 如果new出来的Person对象,就要让程序员自觉的去释放  delete 有了智能指针,让智能指针托管这个Person对象,对象的释放就不用操心了,让智能指针管理 为了让智能指针想普通的Person*指针一样使用 就要重载 -> 和* #define _CRT_SECURE_NO_WARNI...

embed 元素的用法

embed (一)、基本语法: embed src=url 说明:embed可以用来插入各种多媒体,格式可以是 Midi、Wav、AIFF、AU、MP3等等,Netscape及新版的IE 都支持。url为音频或视频文件及其路径,可以是相对路径或绝对路径。 示例:<embed src="http://t.zoukankan.com/your.mid"&...

解决flv格式视频在网页中播放问题

<embed type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" quality="high" allowfullscreen="true" src=http://192.168.40.135/FlvPla...

VUE三 vue-router(路由)详解

前端路由 根据不同的 url 地址展示不同的内容或页面,无需依赖服务器根据不同URL进行页面展示操作 优点 用户体验好,不需要每次都从服务器全部获取,快速展现给用户 缺点 使用浏览器的前进,后退键的时候会重新发送请求,没有合理地利用缓存 单页面无法记住之前滚动的位置,无法在前进,后退的时候记住滚动的位置 一、路由(以user为例) userList--...

html基础:基本标签

一、html简介 html是一个长的字符串,它能够被浏览器解析。html分为三块:html代码,css,js。 html的注释可以用<!-- --> 或者ctrl+? html页面打开以后,右键-检查--直接点击刷新不会清除缓存。如果想要清楚缓存,在检查页面打开的前提下,在浏览器刷新按钮上右键--清除缓存并硬性重新加载 即可 二、标签 标签分为...