大伙好,打扰大家几分钟:我的spring 和Hibernate整合时,只要添加上声明式事务就会报错(不添加没有问题):错误是ActionBean(name="/users")错误如下:org.springframework.beans.factory.BeanCreationException: Error creating bean with name '/users' defined in class path resource [applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.TypeMismatchException: Failed to convert property value of type [$Proxy2] to required type [dao.UserDAO] for property 'userDao'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [$Proxy2] to required type [dao.UserDAO] for property 'userDao': no matching editors or conversion strategy found applicationContext.xml配置如下:<?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="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.microsoft.jdbc.sqlserver.SQLServerDriver">
</property>
<property name="url"
value="jdbc:microsoft:sqlserver://localhost:1433">
</property>
<property name="username" value="sa"></property>
<property name="password" value="sa"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect
</prop>
<prop key="sql_">true</prop>
</props>
</property>
<property name="mappingResources">
<list>
<value>entity/Login.hbm.xml</value></list>
</property>
</bean>

<bean id="userDaoTarget" class="dao.UserDAO">
<property name="sessionFactory" ref="sessionFactory">
</property>
</bean>
<!-- userAction -->
<bean name="/users" class="com.test2.struts.action.UserAction">
<property name="userDao" ref="daoProxy">
</property>
</bean>
<!-- 声明事务管理器 -->
<bean id="myTranscationBean" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 这是声明代理bean-->
<bean id="daoProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="myTranscationBean"> </property>
<property name="target" ref="userDaoTarget"></property>
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>

</beans>struts_config.xml配置如下:<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd"><struts-config>
  <data-sources />
  <form-beans >
    <form-bean name="userForm" type="org.apache.struts.action.DynaActionForm" >
    <form-property name="item" type="entity.Login"></form-property>
    </form-bean>
     </form-beans>  <global-exceptions />
  <global-forwards />
  <action-mappings >
    <action
      attribute="userForm"
      name="userForm"
      path="/users"
      scope="request"
      type="org.springframework.web.struts.DelegatingActionProxy">
      <forward name="result" path="/result.jsp" />
    </action>
  </action-mappings>

  <message-resources parameter="com.test2.struts.ApplicationResources" />
  <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
   <set-property property="contextConfigLocation" value="classpath:applicationContext.xml" />
  </plug-in>
</struts-config>

解决方案 »

  1.   

    错误无关事务,也许是Jar包冲突,是否存在重复的Jar?
      

  2.   

    我的事务管理:下面的代码在其他项目里面用过,没有错误,
    <!-- 声明事务管理器 --> 
    <bean id="myTranscationBean" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
    <property name="sessionFactory" ref="sessionFactory"> </property> 
    </bean> 
    <!-- 这是声明代理bean--> 
    <bean id="daoProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> 
    <property name="transactionManager" ref="myTranscationBean"> </property> 
    <property name="target" ref="userDaoTarget"> </property> 
    <property name="transactionAttributes"> 
    <props> 
    <prop key="*">PROPAGATION_REQUIRED </prop> 
    </props> 
    </property> 
    </bean> 怎么能说注入进去?郁闷!
      

  3.   

    <bean id="userDaoTarget" class="dao.UserDAO"> 
      <property name="sessionFactory" ref="sessionFactory"> 
      </property> 
    </bean> 你看下你userDaoTarget对应的class路径是否正确,应该是找不到dao.UserDAO