0

My Application uses Spring 3 + MyBatis 3. I'm trying to setup multiple database connection (MySQL and Oracle), and I got this message.

Error creating bean with name 'MySQLService' defined in ServletContext resource [/WEB-INF/appContext-database.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'MySQLDao' of bean class [com.ekalife.MyGadget.service.MainService]: Bean property 'MySQLDao' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?

My setup appContext-database.xml

<!-- ========== Setting Database and Connection Pooling ========== -->

<bean id="dataSourceMySQL" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" 
    p:driverClass="${jdbc.driver}" 
    p:jdbcUrl="${jdbc.url}" 
    p:user="${jdbc.user}" 
    p:password="${jdbc.password}" 
    p:idleConnectionTestPeriod="3600" 
    p:preferredTestQuery="SELECT * FROM HD_USERS" 
    p:acquireRetryAttempts="50" 
    p:autoCommitOnClose="false" /> 

<bean id="dataSourceORA" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"
    p:driverClass="${jdbc.driverORA}"
    p:jdbcUrl="${jdbc.urlORA}"
    p:user="${jdbc.userORA}"
    p:password="${jdbc.passwordORA}"
    p:idleConnectionTestPeriod="3600"
    p:preferredTestQuery="SELECT SYSDATE FROM DUAL"
    p:acquireRetryAttempts="50"
    p:autoCommitOnClose="false"/>

<!-- transaction manager -->
<bean id="MySQLService" class=" com.ekalife.MyGadget.service.MainService">
    <property name="MySQLDao" ref="MySQLDao"/>
</bean>

<bean id="ORAService" class=" com.ekalife.MyGadget.service.OraService">
    <property name="ORADao" ref="ORADao"/>
</bean>

<!-- MySQL transactional advice -->
<tx:advice id="mySqlTxAdvice" transaction-manager="transactionManagerMySQL">
    <tx:attributes>
        <tx:method name="select" read-only="true" />
        <tx:method name="view" read-only="true" />
        <tx:method name="*" />
    </tx:attributes>
</tx:advice>
<aop:config>
    <aop:pointcut id="mySQLManagerMethods" expression="execution(* com.ekalife.MyGadget.service.MainService.*(..))"/>
    <aop:advisor advice-ref="mySqlTxAdvice" pointcut-ref="mySQLManagerMethods" />
</aop:config>
<bean id="transactionManagerMySQL" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSourceMySQL" />
</bean>

<!-- Oracle transactional advice -->
<tx:advice id="oraTxAdvice" transaction-manager="transactionManagerORA">
    <tx:attributes>
        <tx:method name="select" read-only="true" />
        <tx:method name="view" read-only="true" />
        <tx:method name="*" />
    </tx:attributes>
</tx:advice>
<aop:config>
    <aop:pointcut id="oraManagerMethods" expression="execution(* com.ekalife.MyGadget.service.OraService.*(..))" />
    <aop:advisor advice-ref="oraTxAdvice" pointcut-ref="oraManagerMethods" />
</aop:config>
<bean id="transactionManagerORA" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSourceORA"/>
</bean>

<bean id="MySQLDao" class="org.mybatis.spring.SqlSessionFactoryBean"
    p:dataSource-ref="dataSourceMySQL"
    p:mapperLocations="classpath*:com/ekalife/MyGadget/dao/MySQL/**/*.xml"
    p:typeAliasesPackage="com.ekalife.MyGadget.model" />
<bean id="MySQLDaoSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
    <constructor-arg ref="MySQLDao"/>
</bean>

<bean id="ORADao" class="org.mybatis.spring.SqlSessionFactoryBean"
    p:dataSource-ref="dataSourceORA" 
    p:mapperLocations="classpath*:com/ekalife/MyGadget/dao/Ora/**/*.xml"
    p:typeAliasesPackage="com.ekalife.MyGadget.model" />
<bean id="ORADaoSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
    <constructor-arg ref="ORADao"/>
</bean>

<bean id="MySQLMapper" class="org.mybatis.spring.mapper.MapperFactoryBean" 
    p:sqlSessionTemplate-ref="MySQLDaoSessionTemplate"
    p:mapperInterface="com.ekalife.MyGadget.dao.MainMapper"
    p:sqlSessionFactory-ref="MySQLDao"/>
<bean id="ORAMapper" class="org.mybatis.spring.mapper.MapperFactoryBean" 
    p:sqlSessionTemplate-ref="ORADaoSessionTemplate"
    p:mapperInterface="com.ekalife.MyGadget.dao.OraMapper"
    p:sqlSessionFactory-ref="ORADao"/>  

MySQL Service

package com.ekalife.MyGadget.service;

@Transactional
public class MainService {

    private static Logger logger = Logger.getLogger(MainService.class);

    @Autowired
    private MainMapper mainMapper;

Oracle Service

@Transactional
public class OraService {

    private static Logger logger = Logger.getLogger(MainService.class);

    @Autowired
    private OraMapper oraMapper;

Thanks in advance :)

4

2 回答 2

0

您收到错误的主要原因是您的 Mainservice 和 OraService 类没有 getter 和 setter。它甚至会告诉您 Spring 错误“不可写或具有无效的 setter 方法”。

于 2013-02-05T21:26:00.863 回答
0

你应该MySQLDao在你的MainService和它的 setter 和 getter 中声明,因为 Spring 正在寻找它。

另一个错误是变量名应该以小写开头:

<bean id="MySQLService" class=" com.ekalife.MyGadget.service.MainService">
    <property name="mySQLDao" ref="MySQLDao"/>
</bean>

另外,我看到一些不一致的地方。你MainService有一个属性mainMapper,它是自动装配的,而且在你想要 inyect 的应用程序上下文 xml 中mySQLDao

最好为所有注射选择相同的策略。例如,我建议你在 xml 配置中 inyect all:

<bean id="MySQLService" class=" com.ekalife.MyGadget.service.MainService">
    <property name="mySQLDao" ref="MySQLDao"/>
    <property name="mainMapper" ref="MySQLMapper"/>
</bean>

已更新:回答您的问题“如何在 MainService 中声明 MySQLDao”

package com.ekalife.MyGadget.service;

// imports...

@Transactional
public class MainService {

    private static Logger logger = Logger.getLogger(MainService.class);

    private MainMapper mainMapper;

    private SqlSessionFactoryBean mySQLDao;

    // your methods

    // setter and getter methods for mySQLDao and mainMapper

}
于 2012-04-10T17:25:03.397 回答