设置属性值时出错(Spring 3和mybatis 3)
我的应用程序使用Spring3+Mybatis3。我正在尝试设置多个数据库连接(MySQL和Oracle),我收到了这条消息设置属性值时出错(Spring 3和mybatis 3),spring,mybatis,Spring,Mybatis,我的应用程序使用Spring3+Mybatis3。我正在尝试设置多个数据库连接(MySQL和Oracle),我收到了这条消息 Error creating bean with name 'MySQLService' defined in ServletContext resource [/WEB-INF/appContext-database.xml]: Error setting property values; nested exception is org.springframework.
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"/>
甲骨文服务
@Transactional
public class OraService {
private static Logger logger = Logger.getLogger(MainService.class);
@Autowired
private OraMapper oraMapper;
提前感谢:)您应该在
MainService
及其setter和getter中声明MySQLDao
,因为Spring正在寻找它
另一个错误是变量名应以lowcase开头:
<bean id="MySQLService" class=" com.ekalife.MyGadget.service.MainService">
<property name="mySQLDao" ref="MySQLDao"/>
</bean>
升级:回答您的问题“如何在MainService中声明MySQLDao”
出现错误的主要原因是您没有Mainservice和OraService类的getter和setter。Spring错误它甚至告诉您in-away“不可写或具有无效的setter方法”。但我在服务类中看不到具有mysqldao的属性及其setter方法:(,它是否与@transaction相关?或者应该是,它必须是@Transactional(“mysqldao”)公共类MainService?如何在MainService中声明MySQLDao?有没有一种在Spring中设置多个连接(mysql和oracle)的简单方法?请参阅我的更新,了解如何声明MySQLDao。没有一种在Spring中声明两个数据源的简单方法,至少正如您所期望的那样。我收到了另一条消息“使用ServletContext资源[/WEB-INF/appContext database.xml]中定义的名称'MySQLService'创建bean时出错”:bean初始化失败;嵌套异常为org.springframework.beans.ConversionNotSupportedException:未能将类型为“org.apache.ibatis.session.defaults.DefaultSqlSessionFactory”的属性值转换为属性“mySQLDao”所需的类型“org.mybatis.spring.SqlSessionFactoryBean”:未找到匹配的编辑器或转换策略“看看这些包。类名是相同的,但包不是。你需要解决这个问题。在我的例子中,映射程序包是com.ekalife.MyGadget.dao.MainMapper,服务包是com.ekalife.MyGadget.service.MainService;映射程序是否必须在com.ekalife.MyGadget.service中?因为我制作了另一个appContext-database.xml(没有aop或tx)仅适用于oracle或mysql,并且运行良好。
<bean id="MySQLService" class=" com.ekalife.MyGadget.service.MainService">
<property name="mySQLDao" ref="MySQLDao"/>
</bean>
<bean id="MySQLService" class=" com.ekalife.MyGadget.service.MainService">
<property name="mySQLDao" ref="MySQLDao"/>
<property name="mainMapper" ref="MySQLMapper"/>
</bean>
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
}