spring+struts+ibatis 动态数据源配置 (一)

[align=center][b]spring+ibatis配置多数据源[/b][/align]

说明:最近因为项目需要,经过多方努力和救助,终于实现了spring+ibatis的动态数据源切换配置,在此贴出,以供大家参考,同时希望大家多提意见……

下面是详细代码:
     1.  spring的applicationContext.xml配置文件:
a.  属性文件配置:
<bean id="propertyConfig"  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="location">
   <value>    classpath:resource/DataSourceConfig.properties
   </value>
  </property>
 </bean>

b.  DataSource: C3P0连接池:
【注意:由于存在多数据源,故此步按需要多次配置
<bean id="dataSource"
  class="com.mchange.v2.c3p0.ComboPooledDataSource"
  destroy-method="close">
  <property name="driverClass" value="${gpt.driverClassName}" />
  <property name="jdbcUrl" value="${gpt.url}" />
  <property name="user" value="${gpt.username}" />
  <property name="password" value="${gpt.password}" />
  <property name="minPoolSize" value="${gpt.minPoolSize}" />
  <property name="acquireRetryAttempts"
   value="${gpt.acquireRetryAttempts}" />
  <property name="acquireIncrement"
   value="${gpt.acquireIncrement}" />
  <property name="maxIdleTime" value="${gpt.maxIdleTime}" />
  <property name="initialPoolSize" value="${gpt.initialPoolSize}" />
  <property name="maxPoolSize" value="${gpt.maxPoolSize}" />
  <property name="idleConnectionTestPeriod"
   value="${gpt.idleConnectionTestPeriod}" />
 </bean>

c.  spring事务管理配置:
【红色标记处,多个数据源配置多个】
<bean id="autoProxyCreator"  class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
  <property name="beanNames">
   <value>*Service</value>
  </property>
  <property name="interceptorNames">
   <list><value>transactionInterceptor1</value>   <value>transactionInterceptor2</value>   <value>transactionInterceptor3</value>
   </list>
  </property>
 </bean>
<!-- 标记段重复配置,具体看自己需要 start -->
<bean id="transactionManager"  class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <property name="dataSource" ref="dataSource" />
 </bean>
 <bean id="transactionInterceptor1"  class="org.springframework.transaction.interceptor.TransactionInterceptor">
  <property name="transactionManager" ref="transactionManager" />
  <property name="transactionAttributes">
   <props>
    <prop key="insert*">PROPAGATION_REQUIRED</prop>
    <prop key="delete*">PROPAGATION_REQUIRED</prop>
    <prop key="update*">PROPAGATION_REQUIRED</prop>
    <prop key="do*">PROPAGATION_REQUIRED</prop>
   </props>
  </property>
 </bean>
<!-- end -->

d.  ibatis sql map 配置:
【同理,有多少个数据源就配置多少遍】
<bean id="sqlMapClient"  class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
  <property name="dataSource" ref="dataSource" />
  <property name="configLocation"   value="classpath:com/sqlmap/sqlmap-config.xml" />

就这样,spring配置文件就准备好了。
 </bean>

你可能感兴趣的:(spring,bean,ibatis,struts,配置管理)