2 asiangbj Asiangbj 于 2016.09.27 11:02 提问

spring+springmvc+mybatis实现atomikos多数据源的问题

spring-mybatis.xml文件:
<!-- 配置多个数据源 -->





localhost
3306
ted
tkpomuser
77SHIbian08







<!-- 配置多个数据源 -->
<bean id="oracleDataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean" init-method="init" destroy-method="close">
    <property name="uniqueResourceName" value="oracleDataSource"/>
    <property name="xaDataSourceClassName" value="oracle.jdbc.xa.client.OracleXADataSource"/>
    <property name="xaProperties">
        <props>
            <prop key="URL">jdbc:oracle:thin:@localhost:1521:ORCL</prop>
            <prop key="user">test</prop>
            <prop key="password">test</prop>
        </props>
    </property>
    <property name="minPoolSize" value="${c3p0.minPoolSize}" />
    <property name="maxPoolSize" value="${c3p0.maxPoolSize}" />
    <property name="borrowConnectionTimeout" value="30" />
    <property name="testQuery" value="select 1" />
    <property name="maintenanceInterval" value="60" />
</bean>

<bean id="mysqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="mysqlDataSource" />
    <property name="configLocation" value="classpath:config/mybatis-configuration.xml" />
    <property name="mapperLocations" value="classpath*:com/it/taikang/modules/**/**/*.xml"/>
</bean>

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
    <property name="basePackage" value="com.it.taikang.modules.**.dao"/>
    <property name="sqlSessionFactoryBeanName" value="mysqlSessionFactory" />
</bean>

<bean id="oracleSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="oracleDataSource" />
    <property name="configLocation" value="classpath:config/mybatis-configuration-oracle.xml" />
    <property name="mapperLocations" value="classpath*:com/it/taikang/oracle/**/**/*.xml"/>
</bean>

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.it.taikang.oracle.**.dao"/>
    <property name="sqlSessionFactoryBeanName" value="oracleSessionFactory" />
</bean>


BaseDao:
@Repository

public class BaseDao extends SqlSessionDaoSupport {

 @Resource
 public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory){
     super.setSqlSessionFactory(sqlSessionFactory);
 }
}

错误详情如下:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'baseDao': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.apache.ibatis.session.SqlSessionFactory] is defined: expected single matching bean but found 2: mysqlSessionFactory,oracleSessionFactory
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:306)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1146)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:628)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:389)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:294)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5068)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5584)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:899)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:875)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:652)
at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1863)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:618)
at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:565)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:301)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1487)
at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:97)
at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1328)
at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1420)
at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:848)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:322)
at sun.rmi.transport.Transport$1.run(Transport.java:177)
at sun.rmi.transport.Transport$1.run(Transport.java:174)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:173)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:556)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:811)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:670)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.apache.ibatis.session.SqlSessionFactory] is defined: expected single matching bean but found 2: mysqlSessionFactory,oracleSessionFactory
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:865)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:770)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:440)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:418)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:546)
at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:159)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:303)
... 56 more
跪求大神

2个回答

Farrell_zeng
Farrell_zeng   2016.09.27 11:10
<bean id="abstractXADataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean"
    init-method="init" destroy-method="close" abstract="true">
    <property name="xaDataSourceClassName"
        value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" />
    <property name="poolSize" value="10" />
    <property name="minPoolSize" value="10" />
    <property name="maxPoolSize" value="30" />
    <property name="borrowConnectionTimeout" value="60" />
    <property name="reapTimeout" value="20" />
    <!-- 最大空闲时间 -->
    <property name="maxIdleTime" value="60" />
    <property name="maintenanceInterval" value="60" />
    <property name="loginTimeout" value="60" />
    <property name="testQuery">
        <value>select 1</value>
    </property>
</bean>

<!--数据源-->
<bean id="dataSourceYxb" parent="abstractXADataSource">
     <!-- value只要两个数据源不同就行,随便取名 -->
    <property name="uniqueResourceName" value="mysql/sitestone_yxb" />
     <property name="xaDataSourceClassName"
               value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" />
     <property name="xaProperties">
         <props>
             <prop key="URL">${jdbc.url}</prop>
             <prop key="user">${jdbc.username}</prop>
             <prop key="password">${jdbc.password}</prop>
             <prop key="pinGlobalTxToPhysicalConnection">true</prop>
         </props>
     </property>
 </bean>

<!--  数据源 -->
 <bean id="dataSourceDB" parent="abstractXADataSource">
     <!-- value只要两个数据源不同就行,随便取名 -->
     <property name="uniqueResourceName" value="mysql/sitestone_db" />
     <property name="xaDataSourceClassName"
               value="com.mysql.jdbc.jdbc2.optional.MysqlXADataSource" />
     <property name="xaProperties">
         <props>
             <prop key="URL">${jdbc.url.db}</prop>
             <prop key="user">${jdbc.username.db}</prop>
             <prop key="password">${jdbc.password.db}</prop>
             <prop key="pinGlobalTxToPhysicalConnection">true</prop>
         </props>
     </property>
 </bean>

<!-- MyBatis配置 -->
<bean id="sqlSessionFactoryYXB" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSourceYxb" />
    <!-- <property name="configLocation" value="classpath:mapper/Configuration.xml"/> -->
    <!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->
    <property name="typeAliasesPackage" value="com.test.yxb.pojo.yxb.model,com.test.yxb.api.pojo.yxb.model" />
    <!-- 显式指定Mapper文件位置 -->
    <property name="mapperLocations">
        <list>
            <value>classpath:/mapper/yxb/*Mapper.xml</value>
            <value>classpath:/mapper/api/yxb/*Mapper.xml</value>
        </list>
    </property>
</bean>

<bean id="sqlSessionFactoryDB" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSourceDB" />
    <!-- <property name="configLocation" value="classpath:mapper/Configuration.xml"/> -->
    <!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->
    <property name="typeAliasesPackage" value="com.test.yxb.pojo.db.model,com.test.yxb.api.pojo.db.model" />
    <!-- 显式指定Mapper文件位置 -->
    <property name="mapperLocations">
        <list>
            <value>classpath:/mapper/db/*Mapper.xml</value>
            <value>classpath:/mapper/api/db/*Mapper.xml</value>
        </list>
    </property>
</bean>

<!-- 扫描basePackage下所有以@MyBatisRepository标识的 接口 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.test.yxb.persistence.dao.yxb,com.test.yxb.api.dao.yxb" />
    <property name="annotationClass" value="com.test.framework.orm.mybatis.MyBatisRepository" />
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryYXB"/>
</bean>

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.test.yxb.persistence.dao.db,com.test.yxb.api.dao.db"/>
    <property name="annotationClass" value="com.test.framework.orm.mybatis.MyBatisRepository" />
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryDB"/>
</bean>

<!-- 开启注解事务 只对当前配置文件有效 -->
<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="atomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager"
    init-method="init" destroy-method="close">
    <property name="forceShutdown">
        <value>true</value>
    </property>
</bean>

<bean id="atomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
    <property name="transactionTimeout" value="300" />
</bean>

<!-- JTA事务管理 -->
<bean id="transactionManager"
    class="org.springframework.transaction.jta.JtaTransactionManager">
    <property name="transactionManager">
        <ref bean="atomikosTransactionManager" />
    </property>
    <property name="userTransaction">
        <ref bean="atomikosUserTransaction" />
    </property>
    <!-- 必须设置,否则程序出现异常 JtaTransactionManager does not support custom isolation 
        levels by default -->
    <property name="allowCustomIsolationLevels" value="true" />
</bean>

<!-- 开启AOP监听 只对当前配置文件有效 -->
<aop:aspectj-autoproxy expose-proxy="true" />

<!-- jta事务管理 read-only没起作用须要测试 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="do*" propagation="REQUIRED" />
        <tx:method name="save*" propagation="REQUIRED" />
        <tx:method name="update*" propagation="REQUIRED" />
        <tx:method name="delete*" propagation="REQUIRED" />
        <tx:method name="process*" propagation="REQUIRED" />
        <tx:method name="inser*" propagation="REQUIRED" />
        <tx:method name="*" propagation="NOT_SUPPORTED" read-only="true" />
    </tx:attributes>
</tx:advice>

<aop:config expose-proxy="true">
    <!-- 只对业务逻辑层实施事务,并处理基类里面的service -->
    <aop:pointcut id="txPointcut"
        expression="execution(* com.test.yxb.business.service.*.*(..)) or execution(* com.test.yxb.api.service.*.*(..))" />
    <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" />
</aop:config>
Asiangbj
Asiangbj   2016.09.27 13:22

主要是BaseDao类中,怎么将session注入???

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!