Invalid property 'transactionManager' of bean class的解决办法

Invalid property 'transactionManager' of bean class的解决办法
2008-06-25 09:22

一个关于struts + spring + hibernate整合事务配置的问题(请教高手)
... ...
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
   <property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<bean id="txProxyTemplate" abstract="true"
    class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
   <property name="transactionManager" ref="transactionManager"></property>
   <property name="transactionAttributes">
    <props>
     <prop key="create*">PROPAGATION_REQUIRED</prop>
     <prop key="update*">PROPAGATION_REQUIRED</prop>
     <prop key="delete*">PROPAGATION_REQUIRED</prop>
     <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
    </props>
   </property>
</bean>

<bean id="userDAO" class="dgut.ke.dao.impl.UserDAO">
       <property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="userService" parent="txProxyTemplate"   class="dgut.ke.service.impl.UserService">
     <property name="userDAO" ref="userDAO" />
</bean>
... ....
启动Tomecat时出了错.之前没有添加事务处理时.上面的代码是可以正常运行的.添加事务之后却出现以下错误
ERROR - Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService' defined in ServletContext resource [/WEB-INF/applicationContext-hibernate.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'transactionManager' of bean class [dgut.ke.service.impl.UserService]: Bean property 'transactionManager' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
Caused by:
org.springframework.beans.NotWritablePropertyException: Invalid property 'transactionManager' of bean class [dgut.ke.service.impl.UserService]: Bean property 'transactionManager' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
解决办法

<bean id="userService" parent="txProxyTemplate"   class="dgut.ke.service.impl.UserService">
     <property name="userDAO" ref="userDAO" />
</bean>
换成以下代码就可以正常运行了
<bean id="userService" parent="txProxyTemplate">
       <property name="target">
            <bean class="dgut.ke.service.impl.UserService">
                 <property name="userDAO" ref="userDAO"></property>
            </bean>
       </property>
</bean>

查看代码是否有问题<!-- 修改后的Spring配置文件 --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 加载数据库配置文件 --> <context:property-placeholder location="classpath:config/db.properties" /> <!-- 修改后的数据源配置(关键修改点) --> <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"> <!-- 1. 更新驱动类名 --> <property name="driverClassName" value="${jdbc.driver}" /> <!-- 2. 确保URL包含时区参数和时区支持 --> <property name="url" value="${jdbc.url}?serverTimezone=Asia/Shanghai&useSSL=false&allowPublicKeyRetrieval=true" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!-- 连接池配置建议调整 --> <property name="maxTotal" value="${jdbc.maxTotal}" /> <property name="maxIdle" value="${jdbc.maxIdle}" /> <property name="initialSize" value="${jdbc.initialSize}" /> </bean> <!-- 其他保持不变的配置 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <tx:annotation-driven transaction-manager="txManager" /> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:config/mybatis-config.xml" /> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.mybatis.mapper" /> <property name="sqlSess
最新发布
03-28
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值