Spring mvc动态多数据源
来源:互联网 发布:windows自带画图软件 编辑:程序博客网 时间:2024/06/11 09:32
本文基于Spring MVC,拦截器实现Session控制。
本文通过拦截器取得当前使用的Locale,然后通过Locale找到不同的数据源。
首先,新建类DynamicDataSource,使其继承org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource并实现其determineCurrentLookupKey方法,并实现获取currentLookupKey的方法,代码如下所示:
- /**
- *
- * @author geloin
- * @date 2012-5-18 下午3:20:51
- */
- package com.embest.ruisystem.datasource;
- import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
- /**
- * 动态数据源
- *
- * @author geloin
- * @date 2012-5-18 下午3:20:51
- */
- public class DynamicDataSource extends AbstractRoutingDataSource {
- private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();
- /**
- *
- * @author geloin
- * @date 2012-5-18 下午4:06:44
- * @return the currentLookupKey
- */
- public static String getCurrentLookupKey() {
- return (String) contextHolder.get();
- }
- /**
- *
- * @author geloin
- * @date 2012-5-18 下午4:06:44
- * @param currentLookupKey
- * the currentLookupKey to set
- */
- public static void setCurrentLookupKey(String currentLookupKey) {
- contextHolder.set(currentLookupKey);
- }
- /*
- * (non-Javadoc)
- *
- * @see
- * org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource#
- * determineCurrentLookupKey()
- */
- @Override
- protected Object determineCurrentLookupKey() {
- return getCurrentLookupKey();
- }
- }
上述代码中的determineCurrentLookupKey方法取得一个字符串,该字符串将与配置文件中的相应字符串进行匹配以定位数据源,配置文件,即applicationContext.xml文件中需要要如下代码:
- <!-- 动态数据源 -->
- lt;bean id="dataSource" class="com.embest.ruisystem.datasource.DynamicDataSource">
- <property name="targetDataSources">
- <map key-type="java.lang.String">
- <entry key="zh" value-ref="chinaDataSource" />
- <entry key="en" value-ref="englishDataSource" />
- </map>
- </property>
- <property name="defaultTargetDataSource" ref="chinaDataSource" />
- lt;/bean>
determineCurrentLookupKey方法取得字符串后,将会与上述配置中的<map...></map>中的值对应,即当determineCurrentLookupKey方法取得值为en时,则数据源指向englishDataSource,当然,map中的value-ref对应的是你在applicationContext.xml文件中配置的数据源,如下所述:
- <!--创建中国jdbc数据源 -->
- <bean id="chinaDataSource" class="org.apache.commons.dbcp.BasicDataSource"
- destroy-method="close">
- <property name="driverClassName" value="${driver_zh}" />
- <property name="url" value="${url_zh}" />
- <property name="username" value="${username_zh}" />
- <property name="password" value="${password_zh}" />
- </bean>
- <!--创建英国jdbc数据源 -->
- <bean id="englishDataSource" class="org.apache.commons.dbcp.BasicDataSource"
- destroy-method="close">
- <property name="driverClassName" value="${driver_en}" />
- <property name="url" value="${url_en}" />
- <property name="username" value="${username_en}" />
- <property name="password" value="${password_en}" />
- </bean>
若determineCurrentLookupKey方法未取得任何值时,则指向defaultTargetDataSource所代表的数据源。
需要注明的是,上述配置中的{url_en}等值来自于jdbc.properties:
- driver_zh=com.mysql.jdbc.Driver
- url_zh=jdbc:mysql://localhost:3306/ruisystem
- username_zh=root
- password_zh=root
- driver_en=com.mysql.jdbc.Driver
- url_en=jdbc:mysql://localhost:3306/ruisystem_en
- username_en=root
- password_en=root
- Locale locale = RequestContextUtils.getLocaleResolver(request) .resolveLocale(request);
- DynamicDataSource.setCurrentLookupKey(locale.getLanguage());
进行上述所有操作后,可以实现国际化数据库,即各个国家使用不同的数据库。
- Spring mvc动态多数据源
- Spring mvc动态多数据源
- Spring mvc实现动态多数据源
- Spring MVC多数据源动态切换配置
- Spring mvc多数据源切换
- spring MVC+MyBatis 多数据源配置
- spring mvc 多数据源的配置
- spring mvc 多数据源配置
- Spring MVC 配置多数据源
- Spring MVC +MyBatis 多数据源配置
- Spring MVC Mybatis多数据源
- Spring mvc 配置mongodb多数据源
- Spring MVC 多数据插入
- Spring动态配置多数据源
- Spring多数据源的动态切换
- Spring动态切换多数据源解决方案
- Spring多数据源的动态切换
- spring动态加载多数据源
- 复习负数的二进制表示方法
- 水晶报表10高级开发版下载及序列号
- ORA-09925 Linux-x86_64 Error: 2: No such file or directory
- .Net 中的托管与非托管 (CLR)
- socket之select函数
- Spring mvc动态多数据源
- 网络字节序与主机字节序
- 不重复的随机数
- 【2012年腾讯俱乐部ACM赛新手组1000】我水平弱爆了—Love Message
- socket的一些基本结构和用法
- 兼容IE、Firefox、Google的回车事件
- Oracle 11g 新特性 -- RMAN Data Recovery Advisor(DRA) 说明
- 使用Fluentd + MongoDB构建实时日志收集系统
- 使用TCP协议的NAT穿透技术 (转)