首页 > 编程语言 > 详细

spring 多数据源配置

时间:2017-01-22 23:49:06      阅读:470      评论:0      收藏:0      [点我收藏+]

<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-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/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">

<tx:annotation-driven />
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:config/env/jdbc.properties" />
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driverClassName}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<!-- 初始化连接大小 -->
<property name="initialSize" value="${dataSource.initialSize}"></property>
<!-- 连接池最大数量 -->
<property name="maxActive" value="${dataSource.maxActive}"></property>
<!-- 连接池最小空闲 -->
<property name="minIdle" value="${dataSource.minIdle}"></property>
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="${dataSource.maxWait}"></property>
</bean>

<bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driverClassName}" />
<property name="url" value="${url2}" />
<property name="username" value="${username2}" />
<property name="password" value="${password2}" />
<!-- 初始化连接大小 -->
<property name="initialSize" value="${dataSource.initialSize}"></property>
<!-- 连接池最大数量 -->
<property name="maxActive" value="${dataSource.maxActive}"></property>
<!-- 连接池最小空闲 -->
<property name="minIdle" value="${dataSource.minIdle}"></property>
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="${dataSource.maxWait}"></property>
</bean>

<!-- 配置 mybatis -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis.xml"/>
<property name="mapperLocations" value="classpath:com/sitech/message/pojo/*.xml"/>
</bean>
<!-- 配置 mybatis2 -->
<bean id="sqlSessionFactory2" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource2"/>
<property name="configLocation" value="classpath:mybatis.xml"/>
<property name="mapperLocations" value="classpath:com/sitech/message/pojo/*.xml"/>
</bean>

<!-- 配置 BaseDao -->
<bean id="baseDao" class="com.sitech.message.dao.impl.BaseDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
<!-- 配置 BaseDao2 -->
<bean id="baseDao2" class="com.sitech.message.dao.impl.BaseDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory2"/>
</bean>

<!-- Mybatis Dao -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.sitech.message.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>

<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>

<!--
name:与事务属性关联的方法名。通配符(*)可以用来指定一批关联到相同的事务属性的方法。 如:‘get*‘、‘handle*‘、‘on*Event‘等等。
propagation="REQUIRED":事务传播行为
isolation 默认值 DEFAULT:事务隔离级别
timeout 默认值 -1 事务超时的时间(以秒为单位)
read-only 默认值 false 事务是否只读?
rollback-for 将被触发进行回滚的 Exception(s);以逗号分开。 如:‘com.foo.MyBusinessException,ServletException‘
no-rollback-for 不被触发进行回滚的 Exception(s);以逗号分开。 如:‘com.foo.MyBusinessException
-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="batch*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception"/>
<tx:method name="query*" propagation="REQUIRED" read-only="true" />
<tx:method name="find*" propagation="REQUIRED" read-only="true" />
<tx:method name="select*" propagation="REQUIRED" read-only="true" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="modify*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>

<!--
把事务控制在Service层
第一个 * —— 通配 任意返回值类型
第二个 * —— 通配 包com.polin.omeal.service下的任意class
第三个 * —— 通配 包com.polin.omeal.service下的任意class的任意方法
第四个 .. —— 通配 方法可以有0个或多个参数

综上:包com.polin.omeal.service下的任意class的具有任意返回值类型、任意类、任意名称的方法、任意数目参数和<tx:advice/>有关的设置
-->
<aop:config>
<aop:pointcut id="aop" expression="execution(* com.sitech.message.*.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="aop"/>
</aop:config>

</beans>

spring 多数据源配置

原文:http://www.cnblogs.com/chafe/p/6341544.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!