Spring mvc动态多数据源

来源:互联网 发布:windows自带画图软件 编辑:程序博客网 时间:2024/06/11 09:32

本文基于Spring MVC,拦截器实现Session控制。

        本文通过拦截器取得当前使用的Locale,然后通过Locale找到不同的数据源。

        首先,新建类DynamicDataSource,使其继承org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource并实现其determineCurrentLookupKey方法,并实现获取currentLookupKey的方法,代码如下所示:

[java] view plaincopy
  1. /** 
  2.  * 
  3.  * @author geloin 
  4.  * @date 2012-5-18 下午3:20:51 
  5.  */  
  6. package com.embest.ruisystem.datasource;  
  7.   
  8. import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;  
  9.   
  10. /** 
  11.  * 动态数据源 
  12.  *  
  13.  * @author geloin 
  14.  * @date 2012-5-18 下午3:20:51 
  15.  */  
  16. public class DynamicDataSource extends AbstractRoutingDataSource {  
  17.   
  18.     private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>();  
  19.   
  20.     /** 
  21.      *  
  22.      * @author geloin 
  23.      * @date 2012-5-18 下午4:06:44 
  24.      * @return the currentLookupKey 
  25.      */  
  26.     public static String getCurrentLookupKey() {  
  27.         return (String) contextHolder.get();  
  28.     }  
  29.   
  30.     /** 
  31.      *  
  32.      * @author geloin 
  33.      * @date 2012-5-18 下午4:06:44 
  34.      * @param currentLookupKey 
  35.      *            the currentLookupKey to set 
  36.      */  
  37.     public static void setCurrentLookupKey(String currentLookupKey) {  
  38.         contextHolder.set(currentLookupKey);  
  39.     }  
  40.   
  41.     /* 
  42.      * (non-Javadoc) 
  43.      *  
  44.      * @see 
  45.      * org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource# 
  46.      * determineCurrentLookupKey() 
  47.      */  
  48.     @Override  
  49.     protected Object determineCurrentLookupKey() {  
  50.         return getCurrentLookupKey();  
  51.     }  
  52.   
  53. }  

        上述代码中的determineCurrentLookupKey方法取得一个字符串,该字符串将与配置文件中的相应字符串进行匹配以定位数据源,配置文件,即applicationContext.xml文件中需要要如下代码:

[java] view plaincopy
  1.       <!-- 动态数据源 -->  
  2. lt;bean id="dataSource" class="com.embest.ruisystem.datasource.DynamicDataSource">  
  3. <property name="targetDataSources">  
  4.     <map key-type="java.lang.String">  
  5.         <entry key="zh" value-ref="chinaDataSource" />  
  6.         <entry key="en" value-ref="englishDataSource" />  
  7.     </map>  
  8. </property>  
  9. <property name="defaultTargetDataSource" ref="chinaDataSource" />  
  10. lt;/bean>  

        determineCurrentLookupKey方法取得字符串后,将会与上述配置中的<map...></map>中的值对应,即当determineCurrentLookupKey方法取得值为en时,则数据源指向englishDataSource,当然,map中的value-ref对应的是你在applicationContext.xml文件中配置的数据源,如下所述:

[java] view plaincopy
  1.        <!--创建中国jdbc数据源 -->  
  2. <bean id="chinaDataSource" class="org.apache.commons.dbcp.BasicDataSource"  
  3.     destroy-method="close">  
  4.     <property name="driverClassName" value="${driver_zh}" />  
  5.     <property name="url" value="${url_zh}" />  
  6.     <property name="username" value="${username_zh}" />  
  7.     <property name="password" value="${password_zh}" />  
  8. </bean>  
  9. <!--创建英国jdbc数据源 -->  
  10. <bean id="englishDataSource" class="org.apache.commons.dbcp.BasicDataSource"  
  11.     destroy-method="close">  
  12.     <property name="driverClassName" value="${driver_en}" />  
  13.     <property name="url" value="${url_en}" />  
  14.     <property name="username" value="${username_en}" />  
  15.     <property name="password" value="${password_en}" />  
  16. </bean>  

        若determineCurrentLookupKey方法未取得任何值时,则指向defaultTargetDataSource所代表的数据源。

        需要注明的是,上述配置中的{url_en}等值来自于jdbc.properties:

[java] view plaincopy
  1. driver_zh=com.mysql.jdbc.Driver  
  2. url_zh=jdbc:mysql://localhost:3306/ruisystem  
  3. username_zh=root  
  4. password_zh=root  
  5.   
  6. driver_en=com.mysql.jdbc.Driver  
  7. url_en=jdbc:mysql://localhost:3306/ruisystem_en  
  8. username_en=root  
  9. password_en=root  
        以上工作做好后,还差最后一步,即何时给determineCurrentLookupKey()方法传值。通过DynamicDataSource类可知,该方法的值可通过外办调用setCurrentLookupKey方法设置,作者在拦截器中添加如下代码进行设置:

[java] view plaincopy
  1. Locale locale = RequestContextUtils.getLocaleResolver(request) .resolveLocale(request);  
  2. DynamicDataSource.setCurrentLookupKey(locale.getLanguage());  

        进行上述所有操作后,可以实现国际化数据库,即各个国家使用不同的数据库。
原创粉丝点击