现在的位置: 首页 > 综合 > 正文

学习笔记_spring多数据源配置

2013年10月15日 ⁄ 综合 ⁄ 共 4332字 ⁄ 字号 评论关闭

项目中我们经常会遇到多数据源的问题,尤其是数据同步或定时任务等项目更是如此。多数据源让人最头痛的,不是配置多个数据源,而是如何能灵活动态的切换数据源。例如在一个spring和hibernate的框架的项目中,我们在spring配置中往往是配置一个dataSource来连接数据库,然后绑定给sessionFactory,在dao层代码中再指定sessionFactory来进行数据库操作。

正如上图所示,每一块都是指定绑死的,如果是多个数据源,也只能是下图中那种方式。

可看出在Dao层代码中写死了两个SessionFactory,这样日后如果再多一个数据源,还要改代码添加一个SessionFactory,显然这并不符合开闭原则。

那么正确的做法应该是

代码如下:

1. applicationContext.xml

[html] view
plain
copy

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"  
  4.     xmlns:cache="http://www.springframework.org/schema/cache"  
  5.     xmlns:context="http://www.springframework.org/schema/context"  
  6.     xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"  
  7.     xmlns:jms="http://www.springframework.org/schema/jms" xmlns:lang="http://www.springframework.org/schema/lang"  
  8.     xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:oxm="http://www.springframework.org/schema/oxm"  
  9.     xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task"  
  10.     xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"  
  11.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd    
  12.     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd    
  13.     http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd    
  14.     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd    
  15.     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd    
  16.     http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd    
  17.     http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.1.xsd    
  18.     http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.1.xsd    
  19.     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd    
  20.     http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.1.xsd    
  21.     http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd    
  22.     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd    
  23.     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd">  
  24.   
  25.     <context:annotation-config />  
  26.   
  27.     <context:component-scan base-package="com"></context:component-scan>  
  28.   
  29.     <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  30.         <property name="locations">  
  31.             <list>  
  32.                 <value>classpath:com/resource/config.properties</value>  
  33.             </list>  
  34.         </property>  
  35.     </bean>  
  36.   
  37.     <bean id="dataSourceOne" class="com.mchange.v2.c3p0.ComboPooledDataSource"  
  38.         destroy-method="close">  
  39.         <property name="driverClass" value="${dbOne.jdbc.driverClass}" />  
  40.         <property name="jdbcUrl" value="${dbOne.jdbc.url}" />  
  41.         <property name="user" value="${dbOne.jdbc.user}" />  
  42.         <property name="password" value="${dbOne.jdbc.password}" />  
  43.         <property name="initialPoolSize" value="${dbOne.jdbc.initialPoolSize}" />  
  44.         <property name="minPoolSize" value="${dbOne.jdbc.minPoolSize}" />  
  45.         <property name="maxPoolSize" value="${dbOne.jdbc.maxPoolSize}" />  
  46.     </bean>  
  47.   
  48.     <bean id="dataSourceTwo" class="com.mchange.v2.c3p0.ComboPooledDataSource"  
  49.         destroy-method="close">  
  50.         <property name="driverClass" value="${dbTwo.jdbc.driverClass}" />  
  51.         <property name="jdbcUrl" value="${dbTwo.jdbc.url}" />  
  52.         <property name="user" value="${dbTwo.jdbc.user}" />  
  53.         <property name="password" value="${dbTwo.jdbc.password}" />  
  54.         <property name="initialPoolSize" value="${dbTwo.jdbc.initialPoolSize}" />  
  55.         <property name="minPoolSize" value="${dbTwo.jdbc.minPoolSize}" />  
  56.         <

抱歉!评论已关闭.