日期:2014-05-17  浏览次数:20790 次

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" />