2012-04-10 39 views
0

我的應用程序使用Spring 3 + MyBatis 3.我試圖設置多個數據庫連接(MySQL和Oracle),並且我收到了此消息。錯誤設置屬性值(春季3 + mybatis 3)

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? 

我的設置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服務

package com.ekalife.MyGadget.service; 

@Transactional 
public class MainService { 

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

    @Autowired 
    private MainMapper mainMapper; 

Oracle服務

@Transactional 
public class OraService { 

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

    @Autowired 
    private OraMapper oraMapper; 

感謝提前:)

+1

但我不能看到一個屬性在服務類mysqldao和setter方法:(,是東西@Transaction有關係嗎?還是應該有 – 2012-04-10 09:05:03

+0

是它必須是 @Transactional(「MySQLDao」) 公衆class MainService? – 2012-04-10 09:20:52

回答

0

你應該在你MainService和setter和getter聲明MySQLDao因爲春天尋找它。

另一個錯誤是,變量名應該與lowcase開始:

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

而且,我看到一些不連貫。您的MainService有一個屬性mainMapper,它是自動佈線的,而且在您想要的應用環境xml中,您需要輸入mySQLDao

最好爲所有的檢測選擇相同的策略。舉例來說,我建議你inyect都在xml配置:

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

UPTATED:在回答你的問題「如何在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 

} 
+0

如何在MainService中聲明MySQLDao?有沒有簡單的方法來在Spring中設置多個連接(mysql和oracle)? – 2012-04-11 01:50:36

+0

查看我的更新,看看如何聲明MySQLDao。沒有一種簡單的方法來聲明兩個數據源 – jddsantaella 2012-04-11 06:17:08

+0

我收到了另一條消息:「在ServletContext資源[/WEB-INF/appContext-database.xml]中定義名稱爲'MySQLService'的bean時出錯:bean初始化失敗;嵌套異常爲org.springframework.beans.ConversionNotSupporte dException:未能將'org.apache.ibatis.session.defaults.DefaultSqlSessionFactory'類型的屬性值轉換爲屬性'mySQLDao'所需的類型'org.mybatis.spring.SqlSessionFactoryBean':未找到匹配的編輯器或轉換策略「 – 2012-04-11 07:02:22

0

主要的原因,你所得到的錯誤是因爲你沒有getter和setter爲你Mainservice和OraService類。 Spring Error甚至告訴你,在離開時「不可寫或具有無效的setter方法」。