1. 程式人生 > >java開源框架整合常見錯誤之Context initialization failed

java開源框架整合常見錯誤之Context initialization failed

錯誤描述:
Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDao' defined in class path resource [applicationContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'txAdvice': Cannot resolve reference to bean 'txManager' while setting bean property 'transactionManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'txManager' 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 [org.springframework.orm.hibernate3.HibernateTransactionManager] to required type [org.hibernate.SessionFactory] for property 'sessionFactory'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [org.springframework.orm.hibernate3.HibernateTransactionManager] to required type [org.hibernate.SessionFactory] for property 'sessionFactory': no matching editors or conversion strategy found
Caused by:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'txAdvice': Cannot resolve reference to bean 'txManager' while setting bean property 'transactionManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'txManager' 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 [org.springframework.orm.hibernate3.HibernateTransactionManager] to required type [org.hibernate.SessionFactory] for property 'sessionFactory'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [org.springframework.orm.hibernate3.HibernateTransactionManager] to required type [org.hibernate.SessionFactory] for property 'sessionFactory': no matching editors or conversion strategy found
Caused by:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'txManager' 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 [org.springframework.orm.hibernate3.HibernateTransactionManager] to required type [org.hibernate.SessionFactory] for property 'sessionFactory'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [org.springframework.orm.hibernate3.HibernateTransactionManager] to required type [org.hibernate.SessionFactory] for property 'sessionFactory': no matching editors or conversion strategy found
Caused by:

錯誤原因:
spring配置檔案中事務配置錯誤:
<bean id="sessionFactory"
  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="configLocation"
   value="classpath:hibernate.cfg.xml">
  </property>
 </bean>


<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="txManager"></property>
應為:
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory" ref="sessionFactory"></property>
 </bean>
 </bean>