spring 多数据源配置

以下是我的xml配置,在配置的过程中涉及到不同的包分配不同的数据源,在这里用逗号分割就好 当然通配符能结局的那是最好了.

还有就是在配置的时候指向MapperScannerConfigurer 的 sqlSessionFactory 的时候 就正常指向就好.网上也有两种指法  这里就不一一说了 .




<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xsi:schemaLocation=" http://www.springframework.org/schema/beans 
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
          http://www.springframework.org/schema/tx 
          http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
          http://www.springframework.org/schema/context 
          http://www.springframework.org/schema/context/spring-context-3.0.xsd 
          http://www.springframework.org/schema/aop 
          http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
          http://www.springframework.org/schema/task 
          http://www.springframework.org/schema/task/spring-task-3.0.xsd
          http://www.springframework.org/schema/rabbit  
    http://www.springframework.org/schema/rabbit/spring-rabbit-1.4.xsd ">
          
    <bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<!-- <value>classpath:init_integration.properties</value> -->
<!-- <value>classpath:mq_integration.properties</value> -->
<!-- <value>classpath:zookeeper_integration.properties</value> -->
<value>classpath:init.properties</value>
<value>classpath:mq.properties</value>
<value>classpath:zookeeper.properties</value>
</list>
</property>
</bean>

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="/WEB-INF/messages/messages" />
<property name="cacheSeconds" value="0" />
</bean>
    
    <bean id="dataSourceAct" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close">
<property name="driverClass" value="${datasource.driverClassName}" />
<property name="jdbcUrl" value="${datasource.act.url}" />
<property name="username" value="${datasource.act.username}" />
<property name="password" value="${datasource.act.password}" />
<property name="idleConnectionTestPeriod" value="${boneCP.act.idleConnectionTestPeriod}" />
<property name="idleMaxAge" value="${boneCP.act.idleMaxAge}" />
<property name="maxConnectionsPerPartition" value="${boneCP.act.maxConnectionsPerPartition}" />
<property name="minConnectionsPerPartition" value="${boneCP.act.minConnectionsPerPartition}" />
<property name="partitionCount" value="${boneCP.act.partitionCount}" />
<property name="acquireIncrement" value="${boneCP.act.acquireIncrement}" />
<property name="statementsCacheSize" value="${boneCP.act.statementsCacheSize}" />
<property name="statementsCachedPerConnection" value="${boneCP.act.statementsCachedPerConnection}" />
<property name="releaseHelperThreads" value="${boneCP.act.releaseHelperThreads}" />
</bean>

<bean id="dataSourceDmain" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close">
<property name="driverClass" value="${datasource.driverClassName}" />
<property name="jdbcUrl" value="${datasource.main.url}" />
<property name="username" value="${datasource.main.username}" />
<property name="password" value="${datasource.main.password}" />
<property name="idleConnectionTestPeriod" value="${boneCP.main.idleConnectionTestPeriod}" />
<property name="idleMaxAge" value="${boneCP.main.idleMaxAge}" />
<property name="maxConnectionsPerPartition" value="${boneCP.main.maxConnectionsPerPartition}" />
<property name="minConnectionsPerPartition" value="${boneCP.main.minConnectionsPerPartition}" />
<property name="partitionCount" value="${boneCP.main.partitionCount}" />
<property name="acquireIncrement" value="${boneCP.main.acquireIncrement}" />
<property name="statementsCacheSize" value="${boneCP.main.statementsCacheSize}" />
<property name="statementsCachedPerConnection" value="${boneCP.main.statementsCachedPerConnection}" />
<property name="releaseHelperThreads" value="${boneCP.main.releaseHelperThreads}" />
</bean>


<!-- datasource 配置jdbcTemplate -->
<bean id="transactionActManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSourceAct"/>
</bean>

<!-- datasource 配置jdbcTemplate -->
<bean id="transactionDmainManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSourceDmain"/>
</bean>

<!-- myBatis配置 活动库-->
<bean id="sqlSessionFactoryAct" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSourceAct" />
</bean>

<!-- myBatis配置 主库-->
<bean id="sqlSessionFactoryDmain" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSourceDmain" />
</bean>


<!-- DAO层由 MapperScannerConfigurer自动生成mapper bean -->
<bean id="actmsc" class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
<property name="sqlSessionFactory" ref="sqlSessionFactoryAct"/> 
<property name="basePackage" 
value="com.act.realize.mapper,
com.act.sys.mapper,
com.act.dfunds.mapper" />
</bean>

<!-- DAO层由 MapperScannerConfigurer自动生成mapper bean  , com.act.user.mapper-->
<bean id="dmsc" class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
<property name="sqlSessionFactory" ref="sqlSessionFactoryDmain"/> 
<property name="basePackage" value="com.act.duser.mapper" />
</bean>

<!-- Service层 -->
<context:component-scan base-package="com.act.*.service.impl"/>

<!-- mq组件 -->
<context:component-scan base-package="com.act.mq.*.*"/>

<!-- aop组件 -->
<context:component-scan base-package="com.act.aop"/>


<!-- 事务 -->
<tx:advice id="txAdviceAct" transaction-manager="transactionActManager">
<tx:attributes>
<tx:method name="select*" read-only="true"/>
<tx:method name="query*" read-only="true"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="*" rollback-for="common.exception.ActException"/>
<tx:method name="*" rollback-for="java.lang.Exception"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:advisor pointcut="execution(* com.act.realize.service.*Service.*(..)) 
|| execution(* com.act.soa.service.*Service.*(..))
|| execution(* com.act.dfunds.service.*Service.*(..))" 
advice-ref="txAdviceAct" />
</aop:config>

<tx:advice id="txAdviceDmain" transaction-manager="transactionDmainManager">
<tx:attributes>
<tx:method name="select*" read-only="true"/>
<tx:method name="query*" read-only="true"/>
<tx:method name="find*" read-only="true"/>
<tx:method name="*" rollback-for="common.exception.ActException"/>
<tx:method name="*" rollback-for="java.lang.Exception"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:advisor pointcut="execution(* com.act.duser.service.*Service.*(..))" advice-ref="txAdviceDmain" />
</aop:config>

</beans>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值