Spring mvc動態多資料來源_SPRING

來源:互聯網
上載者:User

本文基於Spring MVC,攔截器實現Session控制。

        本文通過攔截器取得當前使用的Locale,然後通過Locale找到不同的資料來源。

        首先,建立類DynamicDataSource,使其繼承org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource並實現其determineCurrentLookupKey方法,並實現擷取currentLookupKey的方法,代碼如下所示:

[java]  view plain copy /**   *   * @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檔案中需要要如下代碼:

[java]  view plain copy       <!-- 動態資料源 -->   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檔案中配置的資料來源,如下所述:

[java]  view plain copy        <!--建立中國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:

[java]  view plain copy 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           以上工作做好後,還差最後一步,即何時給determineCurrentLookupKey()方法傳值。通過DynamicDataSource類可知,該方法的值可通過外辦調用setCurrentLookupKey方法設定,作者在攔截器中添加如下代碼進行設定:

[java]  view plain copy Locale locale = RequestContextUtils.getLocaleResolver(request) .resolveLocale(request);   DynamicDataSource.setCurrentLookupKey(locale.getLanguage());  
        進行上述所有操作後,可以實現國際化資料庫,即各個國家使用不同的資料庫。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.