---
title: "Spring+Hibernate框架下Mysql读写分离、主从数据库配置(下)"
author: "meng702"
date: "2011-04-08T23:17:42Z"
tags: []
categories: ["数据库"]
slug: "spring-hibernate-mysql"
draft: false
lastmod: "2011-04-08T23:17:42Z"
id: 993486
url: "https://meng702.iteye.com/blog/993486"
---

转载：[http://lujia35.iteye.com/blog/969466](http://lujia35.iteye.com/blog/969466)

下面配置spring applicationContext.xml文件

Xml代码  [![收藏代码](http://lujia35.iteye.com/images/icon_star.png)](http://lujia35.iteye.com/blog/969466 "收藏这段代码")

01. &lt;?xml version="1.0" encoding="UTF-8"?&gt;
02. &lt;beans xmlns="http://www.springframework.org/schema/beans"
03. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
04. xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
05. xsi:schemaLocation="
06. http://www.springframework.org/schema/beans
07. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
08. http://www.springframework.org/schema/tx
09. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
10. http://www.springframework.org/schema/aop
11. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
12. http://www.springframework.org/schema/context
13. http://www.springframework.org/schema/context/spring-context-3.0.xsd"&gt;
14. &lt;context:annotation-config /&gt;
15. &lt;!-- 自动加载SERVICE DAO ACTION --&gt;
16. &lt;context:component-scan base-package="cn.com.company.dao.\*" /&gt;
17. &lt;context:component-scan base-package="cn.com.company.service.\*" /&gt;
18. &lt;context:component-scan base-package="cn.com.company.action" /&gt;
19. &lt;!-- 加载properties配置文件 --&gt;
20. &lt;bean id="propertyConfigurer"
21. class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"&gt;
22. &lt;property name="locations"&gt;
23. &lt;list&gt;
24. &lt;value&gt;classpath:jdbc.properties&lt;/value&gt;
25. &lt;/list&gt;
26. &lt;/property&gt;
27. &lt;/bean&gt;
28. &lt;bean id="parentDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"&gt;
29. //\*\*\*c3p0配置
30. &lt;/bean&gt;
31. &lt;!-- 主数据源--&gt;
32. &lt;bean id="masterDataSource" parent="parentDataSource"&gt;
33. &lt;property name="driverClass" value="${master.jdbc.driverClassName}" /&gt;
34. &lt;property name="jdbcUrl" value="${master.jdbc.url}" /&gt;
35. &lt;property name="user" value="${master.jdbc.username}" /&gt;
36. &lt;property name="password" value="${master.jdbc.password}" /&gt;
37. &lt;/bean&gt;
38. &lt;!-- 从数据源--&gt;
39. &lt;bean id="slaveDataSource" parent="parentDataSource"&gt;
40. &lt;property name="driverClass" value="${slave.jdbc.driverClassName}" /&gt;
41. &lt;property name="jdbcUrl" value="${slave.jdbc.url}" /&gt;
42. &lt;property name="user" value="${slave.jdbc.username}" /&gt;
43. &lt;property name="password" value="${slave.jdbc.password}" /&gt;
44. &lt;/bean&gt;
45. &lt;bean id="dataSource" class="com.company.datasource.DynamicDataSource"&gt;
46. &lt;property name="targetDataSources"&gt;
47. &lt;map key-type="java.lang.String"&gt;
48. &lt;entry key="slave" value-ref="slaveDataSource" /&gt;
49. &lt;/map&gt;
50. &lt;/property&gt;
51. &lt;property name="defaultTargetDataSource" ref="masterDataSource" /&gt;
52. &lt;/bean&gt;
53. &lt;!-- 配置sessionFactory --&gt;
54. &lt;bean id="sessionFactory"
55. class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"&gt;
56. &lt;property name="dataSource" ref="dataSource"&gt;&lt;/property&gt;
57. &lt;property name="packagesToScan" value="cn.com.company.entity" /&gt;
58. &lt;property name="hibernateProperties"&gt;
59. &lt;props&gt;
60. &lt;prop&gt;//\*\*\*hibernate一些参数这里不写了&lt;/prop&gt;
61. &lt;/props&gt;
62. &lt;/property&gt;
63. &lt;/bean&gt;
64. &lt;!-- 切换数据源 --&gt;
65. &lt;bean id="dataSourceAdvice" class="com.company.aop.DataSourceAdvice" /&gt;
66. &lt;aop:config&gt;
67. &lt;aop:advisor
68. pointcut="execution(* cn.com.company.service..\*Service.\*(..))"
69. advice-ref="dataSourceAdvice" /&gt;
70. &lt;/aop:config&gt;
71. &lt;!-- 配置事务管理器 --&gt;
72. &lt;bean id="transactionManager"
73. class="org.springframework.orm.hibernate3.HibernateTransactionManager"&gt;
74. &lt;property name="sessionFactory"&gt;
75. &lt;ref bean="sessionFactory" /&gt;
76. &lt;/property&gt;
77. &lt;/bean&gt;
78. &lt;!--配置事务的传播特性 --&gt;
79. &lt;tx:advice id="txAdvice" transaction-manager="transactionManager"&gt;
80. &lt;tx:attributes&gt;
81. &lt;!-- 对增、删、改方法进行事务支持 --&gt;
82. &lt;tx:method name="add\*" propagation="REQUIRED" /&gt;
83. &lt;tx:method&lt;