天天看點

struts + spring + hibernate整合事務配置的問題

一個關于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 &apos;userService&apos; defined in ServletContext resource [/WEB-INF/applicationContext-hibernate.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property &apos;transactionManager&apos; of bean class [dgut.ke.service.impl.UserService]: Bean property &apos;transactionManager&apos; 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 &apos;transactionManager&apos; of bean class [dgut.ke.service.impl.UserService]: Bean property &apos;transactionManager&apos; 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>