求解jpa+spring如何配置动态数据源不能基于事物注解
<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
     <property name="persistenceUnitName" value="com.cyyun.ga"/> 
    <!-- <property name="packagesToScan" value="com.cyyun.ga" /> -->
    <property name="persistenceProvider">
        <bean class="org.hibernate.jpa.HibernatePersistenceProvider"/>
    </property>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="generateDdl" value="${jpa_generateDdl}" />
            <property name="database" value="${jpa_database}" />
            <property name="databasePlatform" value="${jpa_databasePlatform}" />
            <property name="showSql" value="${jpa_showSql}" />
        </bean>
    </property>
    <property name="packagesToScan" value="com.cyyun.ga.**.model" />
    <property name="jpaDialect">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
    </property>
    <property name="jpaPropertyMap">
        <map>
            <entry key="javax.persistence.validation.factory" value-ref="validator"/>
        </map>
    </property>
</bean>

<jpa:repositories base-package="com.cyyun.ga.**.repository"
    entity-manager-factory-ref="entityManagerFactory"
    transaction-manager-ref="transactionManager">
</jpa:repositories>

<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />

<!-- JPA transactionManager -->
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
    <qualifier value="com.cyyun.ga"/>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="save*" propagation="REQUIRED"/>
        <tx:method name="update*" propagation="REQUIRED"/>
        <tx:method name="delete*" propagation="REQUIRED"/>
        <tx:method name="batchSave*" propagation="REQUIRED"/>
        <tx:method name="batchDelete*" propagation="REQUIRED"/>
        <tx:method name="get*" propagation="REQUIRED" read-only="true"/>
        <tx:method name="find*" propagation="REQUIRED" read-only="true"/>
        <tx:method name="list*" propagation="REQUIRED" read-only="true"/>
        <tx:method name="query*" propagation="REQUIRED" read-only="true"/>
        <tx:method name="page*" propagation="REQUIRED" read-only="true"/>
        <tx:method name="*" propagation="REQUIRED"/>
    </tx:attributes>
</tx:advice>
<aop:config>
    <aop:pointcut id="txPointcut" expression="execution(* com.cyyun.ga..service..*.*(..))"/>
    <aop:advisor id="txAdvisor" advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>

<bean id="jdbcTemplate"  class="org.springframework.jdbc.core.JdbcTemplate"  >
    <property name="dataSource" ref="dataSource" />
</bean>



<!-- JPA EntityFactory -->
 <bean id="entityManagerFactory1"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource1" />
    <property name="persistenceProvider">
        <bean class="org.hibernate.jpa.HibernatePersistenceProvider"/>
    </property>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="generateDdl" value="${jpa_generateDdl}" />
            <property name="database" value="${jpa_database}" />
            <property name="databasePlatform" value="${jpa_databasePlatform}" />
            <property name="showSql" value="${jpa_showSql}" />
        </bean>
    </property>
    <property name="packagesToScan" value="test.test" />
    <property name="jpaDialect">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
    </property>
    <property name="jpaPropertyMap">
        <map>
            <entry key="javax.persistence.validation.factory" value-ref="validator1"/>
        </map>
    </property>
</bean>

<jpa:repositories base-package="test.test"
    entity-manager-factory-ref="entityManagerFactory1"
    transaction-manager-ref="transactionManager1">
</jpa:repositories>

<bean id="validator1" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />


<bean id="transactionManager1" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory1"/>
    <qualifier value="test.test"/>
</bean>
<tx:advice id="txAdvice1" transaction-manager="transactionManager1">
    <tx:attributes>
        <tx:method name="save*" propagation="REQUIRED"/>
        <tx:method name="update*" propagation="REQUIRED"/>
        <tx:method name="delete*" propagation="REQUIRED"/>
        <tx:method name="batchSave*" propagation="REQUIRED"/>
        <tx:method name="batchDelete*" propagation="REQUIRED"/>
        <tx:method name="get*" propagation="REQUIRED" read-only="true"/>
        <tx:method name="find*" propagation="REQUIRED" read-only="true"/>
        <tx:method name="list*" propagation="REQUIRED" read-only="true"/>
        <tx:method name="query*" propagation="REQUIRED" read-only="true"/>
        <tx:method name="page*" propagation="REQUIRED" read-only="true"/>
        <tx:method name="*" propagation="REQUIRED"/>
    </tx:attributes>
</tx:advice>
<aop:config>
    <aop:pointcut id="txPointcut1" expression="execution(* test.test.*.*(..))"/>
    <aop:advisor id="txAdvisor1" advice-ref="txAdvice1" pointcut-ref="txPointcut1"/>
</aop:config>

<bean id="jdbcTemplate1"  class="org.springframework.jdbc.core.JdbcTemplate"  >
    <property name="dataSource" ref="dataSource1" />
</bean> 



    配置文件这样配的但是在spring启动的时候报错
    No qualifying bean of type 'javax.persistence.EntityManagerFactory' available: expected single matching bean but found 2: entityManagerFactory,entityManagerFactory1

    求大神告知则么配无奈了

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问