本文基於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());
進行上述所有操作後,可以實現國際化資料庫,即各個國家使用不同的資料庫。