ItGo.me - 专注IT技术分享

首页 > Spring > spring配置 > spring下mybatis多数据源配置问题

spring下mybatis多数据源配置问题

时间:2016-07-25来源:网友分享 点击:

这是我的applicationContext.xml文件

<?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:aop="http://www.springframework.org/schema/aop"
     xmlns:tx="http://www.springframework.org/schema/tx"
     xmlns:jdbc="http://www.springframework.org/schema/jdbc"
     xmlns:context="http://www.springframework.org/schema/context"
     xsi:schemaLocation="
      
      
      
      
      ">

<!-- 引入 jdbc.properties -->
<context:property-placeholder location="classpath:jdbc.properties" />

<!-- 配置数据源  -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName"  value="${jdbc.driverClass}" />
<property name="url"  value="${jdbc.url}" />
<property name="username"  value="${jdbc.username}" />
<property name="password"  value="${jdbc.password}" />
<property name="maxActive"  value="${jdbc.maxActive}" />
<property name="maxIdle"  value="${jdbc.maxIdle}" />
<property name="maxWait"  value="${jdbc.maxWait}" />
<property name="poolPreparedStatements" value="${jdbc.poolPreparedStatements}" />
<property name="defaultAutoCommit"  value="${jdbc.defaultAutoCommit}" />
<property name="removeAbandoned"  value="${jdbc.removeAbandoned}" />
<property name="removeAbandonedTimeout" value="${jdbc.removeAbandonedTimeout}" />
</bean>

    <!-- 配置 transaction 处理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

<!-- 启动 ioc 注解 -->
    <context:component-scan base-package="com.brojade" />
    
    <!-- 启动 annotation 事务 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>

<!-- mybatis 拦截器 -->
<bean id="mybatis_common_field_plugin" class="com.brojade.zeus.pub.base.FieldsPlugin"></bean>

<!-- mybaits 工厂类 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
<property name="plugins"><list><ref bean="mybatis_common_field_plugin"/></list></property>
    </bean>

    <!-- mybatis 配置文件和 DAO 的位置  -->
    <bean name="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.brojade.zeus.biz.*.dao" />
    </bean>

    
    <!-- 缓存服务 -->
    <bean class="com.brojade.zeus.pub.cache.CacheServiceLocalImpl" scope="singleton"></bean>
    
    
    <!-- ################################# 交友频道相关配置  ############################################### -->
    <!-- 配置交友频道数据源  -->
<bean id="dataSource_1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName"  value="${jdbc.driverClass}" />
<property name="url"  value="${jiaoyou.jdbc.url}" />
<property name="username"  value="${jdbc.username}" />
<property name="password"  value="${jdbc.password}" />
<property name="maxActive"  value="5000" />
<property name="maxIdle"  value="30" />
<property name="maxWait"  value="1000" />
<property name="poolPreparedStatements" value="true"/>
<property name="defaultAutoCommit"  value="false" />
<property name="removeAbandoned"  value="true" />
<property name="removeAbandonedTimeout" value="5" />
</bean>

    <!-- 配置 transaction 处理器 -->
    <bean id="transactionManager_1" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource_1" />
    </bean>

<!-- 启动 ioc 注解 -->
    <context:component-scan base-package="com.chengjia" />
    
    <!-- 启动 annotation 事务 -->
    <tx:annotation-driven transaction-manager="transactionManager_1"/>
    
<!-- mybaits 工厂类 -->
    <bean id="sqlSessionFactory_1" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource_1" />
    </bean>

    <!-- mybatis 配置文件和 DAO 的位置  -->
    <bean name="mapperScannerConfigurer_1" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.chengjia.jiaoyou.*.dao" />
        <property name="sqlSessionFactory" ref="sqlSessionFactory_1"/>
    </bean>
    
</beans>


但是用JUnit测试就报:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mapperScannerConfigurer_1' defined in class path resource [applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'sqlSessionFactory' of bean class [org.mybatis.spring.mapper.MapperScannerConfigurer]: Bean property 'sqlSessionFactory' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1361)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1086)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:398)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:594)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:407)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at test.luobaolin.CjJyUserTest.before(CjJyUserTest.java:17)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'sqlSessionFactory' of bean class [org.mybatis.spring.mapper.MapperScannerConfigurer]: Bean property 'sqlSessionFactory' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:1024)
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:900)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:76)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:58)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1358)
... 36 more

请问这是什么问题啊,如果谁有多数据源配置的可以发一个,

更多 分享到:


Java中Spring获取bean方法小结

本篇内容主要为大家讲解的是Java中Spring获取bean方法小结。感兴趣的同学可以参考学习下,具体内容如下:...

Hasor 1.1 发布,轻量化 Java 开发框架

Hasor 是一款开源的轻量级 Java 应用程序开发框架,它的核心目标是提供一个简单、切必要的开发环境给开发者,开发...

JTable内容居中显示

1. 软件版本阶段说明* Alpha版: 此版本表示该软件在此阶段主要是以实现软件功能为主,通常只在软件开发者内部交流...

这是我的applicationContext.xml文件 ?xmlversion="1.0"encoding="UTF-8"? beansxmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:t
------分隔线----------------------------