spring 动态数据源切换实例

来源:互联网 发布:恶搞老鼠软件 编辑:程序博客网 时间:2024/06/07 04:50

我们很多项目中业务都需要涉及到多个数据源,最简单的做法就是直接在java代码里面lookup需要的数据源,但是这样的做法很明显耦合度太高了,

而且当逻辑流程不够严谨的时候就会出现各种大家不愿意看到的问题,由于我们现在的大多项目已经离不开spring了,spring也提供各种强大的功能,

很明显这种动态数据源功能也包括在内,具体实现类请看org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource


这里我演示下如何使用spring提供的动态数据源


先写个接口,default设置为null是因为当程序里没有找到相关联的源就会调用默认源

[java] view plain copy
  1. /** 
  2.  * 数据源切换接口 
  3.  *  
  4.  * @author shadow 
  5.  * @create 2013.04.03 
  6.  */  
  7. public interface DataSourceEntry {  
  8.   
  9.     // 默认数据源  
  10.     public final static String DEFAULT_SOURCE = null;  
  11.   
  12.     /** 
  13.      * 还原数据源 
  14.      *  
  15.      * @param joinPoint 
  16.      */  
  17.     public void restore(JoinPoint join);  
  18.   
  19.     /** 
  20.      * 设置数据源 
  21.      *  
  22.      * @param dataSource 
  23.      */  
  24.     public void set(String source);  
  25.   
  26.     /** 
  27.      * 获取数据源 
  28.      *  
  29.      * @return String 
  30.      */  
  31.     public String get();  
  32.   
  33.     /** 
  34.      * 清空数据源 
  35.      */  
  36.     public void clear();  
  37. }  

然后写个实现类,从当前线程里取出对应的数据源名

[java] view plain copy
  1. /** 
  2.  * 数据源切换实现类类 
  3.  *  
  4.  * @author shadow 
  5.  * @create 2013.04.03 
  6.  */  
  7. public class DataSourceEntryImpl implements DynamicTypeEntry {  
  8.   
  9.     private final static ThreadLocal<String> local = new ThreadLocal<String>();  
  10.   
  11.     public void clear() {  
  12.         local.remove();  
  13.     }  
  14.   
  15.     public String get() {  
  16.         return local.get();  
  17.     }  
  18.   
  19.     public void restore(JoinPoint join) {  
  20.         local.set(DEFAULT_SOURCE);  
  21.     }  
  22.   
  23.     public void set(String source) {  
  24.         local.set(source);  
  25.     }  
  26.   
  27. }  


然后写个继承AbstractRoutingDataSource的类,并注入DataSourceEntry,重写determineCurrentLookupKey模版方法

[java] view plain copy
  1. /** 
  2.  * 获取数据源(依赖SPRING框架) 
  3.  *  
  4.  * @author shadow 
  5.  * @create 2013.04.03 
  6.  */  
  7. public class DynamicDataSource extends AbstractRoutingDataSource {  
  8.   
  9.     private DataSourceEntry dataSourceEntry;  
  10.   
  11.     @Override  
  12.     protected Object determineCurrentLookupKey() {  
  13.         return this.dataSourceEntry.get();  
  14.     }  
  15.   
  16.     @Resource  
  17.     public void setDataSourceEntry(DataSourceEntry dataSourceEntry) {  
  18.         this.dataSourceEntry = dataSourceEntry;  
  19.     }  
  20.   
  21. }  

最后就是配置下xml文件,以后只需要直接管理dynamicDataSource这个接口就可以了,至于他内部是哪个数据源是不需要关注,写的切面还原是为了保证每次调用完另外的数据源

都会还原成默认数据源,防止有的人忘记设置回默认的,导致其他代码出问题

[java] view plain copy
  1. <!-- JDBC模板 -->  
  2.     <bean id="jdbcTemplate"  
  3.         class="org.springframework.jdbc.core.JdbcTemplate">  
  4.         <property name="dataSource" ref="dynamicDataSource" />  
  5.     </bean>  
  6.   
  7.     <!-- 获取数据源配置 -->  
  8.     <bean  
  9.         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  10.         <property name="locations">  
  11.             <value>classpath:properties/jdbc.properties</value>  
  12.         </property>  
  13.     </bean>  
  14.   
  15.     <!-- 配置动态数据源 -->  
  16.     <bean id="dynamicDataSource"  
  17.         class="com.shadow.system.base.source.DynamicDataSource">  
  18.         <!-- 通过key-value的形式来关联数据源 -->  
  19.         <property name="targetDataSources">  
  20.             <map key-type="java.lang.String">  
  21.                 <entry value-ref="C3P0_MYSQL" key="C3P0_MYSQL"></entry>  
  22.                 <entry value-ref="C3P0_MYSQL2" key="C3P0_MYSQL2"></entry>  
  23.             </map>  
  24.         </property>  
  25.         <property name="defaultTargetDataSource" ref="C3P0_MYSQL" />  
  26.     </bean>  
  27.   
  28.     <!-- JNDI方式 -->  
  29.     <bean id="C3P0_MYSQL"  
  30.         class="org.springframework.jndi.JndiObjectFactoryBean">  
  31.         <property name="resourceRef">  
  32.             <value>false</value>  
  33.         </property>  
  34.         <property name="jndiName">  
  35.             <value>${JNDI.template}</value>  
  36.         </property>  
  37.     </bean>  
  38.       
  39.     <!-- JNDI方式 -->  
  40.     <bean id="C3P0_MYSQL2"  
  41.         class="org.springframework.jndi.JndiObjectFactoryBean">  
  42.         <property name="resourceRef">  
  43.             <value>false</value>  
  44.         </property>  
  45.         <property name="jndiName">  
  46.             <value>${JNDI.test}</value>  
  47.         </property>  
  48.     </bean>  
  49.   
  50.     <!-- 配置数据源切换实现类 -->  
  51.     <bean id="dataSourceEntry"  
  52.         class="com.shadow.system.base.source.DataSourceEntryImpl" />  
  53.   
  54.     <!-- 切面还原默认数据源 -->  
  55.     <aop:config>  
  56.         <aop:aspect id="dataSourceHolderAdviceAspect"  
  57.             ref="dataSourceEntry">  
  58.             <aop:after method="restore"  
  59.                 pointcut="execution(* com.shadow.mvc.service.*Service.*(..))" />  
  60.         </aop:aspect>  
  61.     </aop:config>  

至于程序里如何变换数据源,你可以在切面上检测哪些方法加入before方法,或者在程序里直接使用DataSourceEntry调用set方法,具体的怎么用不作代码说明了
0 0
原创粉丝点击