当前位置: 代码迷 >> Java Web开发 >> Spring+Hibernate框架上Mysql读写分离、主从数据库配置无法选择数据源
  详细解决方案

Spring+Hibernate框架上Mysql读写分离、主从数据库配置无法选择数据源

热度:10954   发布时间:2013-02-25 21:09:27.0
Spring+Hibernate框架下Mysql读写分离、主从数据库配置无法选择数据源
XML code
<?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/tx                         http://www.springframework.org/schema/tx/spring-tx-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/context                  http://www.springframework.org/schema/context/spring-context-3.0.xsd">    <context:annotation-config />          <!-- 自动加载SERVICE DAO ACTION -->      <context:component-scan base-package="com.test.dao.*" />      <context:component-scan base-package="com.test.service.*" />          <!-- 加载properties配置文件 -->      <bean id="propertyConfigurer"          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">          <property name="locations">              <list>                  <value>classpath:log4j.properties</value>                  <value>classpath:jdbc.properties</value>              </list>          </property>      </bean>        <bean id="parentDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">          <property name="initialPoolSize"><value>30</value></property>        <property name="minPoolSize"><value>10</value></property>        <property name="maxPoolSize"><value>50</value></property>    </bean>       <!-- 主数据源-->      <bean id="masterDataSource" parent="parentDataSource">          <property name="driverClass" value="com.mysql.jdbc.Driver" />          <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test" />          <property name="user" value="root" />          <property name="password" value="" />      </bean>              <!-- 从数据源-->      <bean id="slaveDataSource" parent="parentDataSource">          <property name="driverClass" value="com.mysql.jdbc.Driver" />          <property name="jdbcUrl" value="jdbc:mysql://localhost:3307/test" />          <property name="user" value="root" />          <property name="password" value="" />     </bean>        <bean id="dataSource" class="com.test.datasource.DynamicDataSource">          <property name="targetDataSources">              <map key-type="java.lang.String">                  <entry key="slave" value-ref="slaveDataSource" />                <entry key="master" value-ref="masterDataSource" />              </map>          </property>          <property name="defaultTargetDataSource" ref="masterDataSource" />      </bean>        <!-- 配置sessionFactory -->      <bean id="sessionFactory"          class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">          <property name="dataSource"><ref local="dataSource"/></property>         <property name="packagesToScan" value="com.test.bean" />         <property name="configLocation" value="classpath:hibernate.cfg.xml">        </property>        <property name="hibernateProperties">              <props>                  <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>                 <prop key="hibernate.show_sql">true</prop>                 <prop key="hibernate.format_sql">true</prop>             </props>          </property>      </bean>            <!-- 切换数据源 -->      <bean id="dataSourceAdvice" class="com.test.datasource.aop.DataSourceAdvice"></bean>          <aop:config>          <aop:pointcut id="businessService" expression="execution(* com.test.service..*.*(..))" />        <aop:advisor  pointcut-ref="businessService" advice-ref="dataSourceAdvice" />    </aop:config>          <!-- 配置事务管理器 -->      <bean id="transactionManager"          class="org.springframework.orm.hibernate3.HibernateTransactionManager">          <property name="sessionFactory">              <ref bean="sessionFactory" />          </property>      </bean>          <!--配置事务的传播特性 -->      <tx:advice id="txAdvice" transaction-manager="transactionManager">          <tx:attributes>              <!-- 对增、删、改方法进行事务支持 -->              <tx:method name="add*" propagation="REQUIRED" />              <tx:method name="create*" propagation="REQUIRED" />              <tx:method name="save*" propagation="REQUIRED" />              <tx:method name="edit*" propagation="REQUIRED" />              <tx:method name="update*" propagation="REQUIRED" />              <tx:method name="delete*" propagation="REQUIRED" />              <tx:method name="remove*" propagation="REQUIRED" />              <!-- 对查找方法进行只读事务 -->              <tx:method name="loadByUsername*" propagation="SUPPORTS" read-only="true" />              <!-- 对其它方法进行只读事务 -->              <tx:method name="*" propagation="SUPPORTS" read-only="true" />          </tx:attributes>      </tx:advice>          <!--那些类的哪些方法参与事务 -->      <aop:config>          <aop:advisor              pointcut="execution(* com.test.service..*.*(..))"              advice-ref="txAdvice" />      </aop:config>         <!-- 配置DAO -->    <bean id="personDAO" class="com.test.dao.impl.PersonDAOImpl">        <property name="sessionFactory">            <ref local="sessionFactory"></ref>        </property>    </bean>        <!-- 配置service -->    <bean id="personService" class="com.test.service.impl.PersonServiceImpl">        <property name="personDAO">            <ref bean="personDAO"></ref>        </property>    </bean></beans>
  相关解决方案