NoSuchBeanDefinitionException:没有名为' transactionManager'被定义为

时间:2014-06-17 21:41:18

标签: eclipse spring transactionmanager

我使用spring和hibernate开发应用程序。

当我运行我的应用程序时,我收到此错误消息:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'transactionManager' is defined

在我的上下文应用程序文件中,我有:

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

我搜索了这个问题,我找到了一个解决方案,我必须改变这一行:

<bean id="tansactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">

通过:

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">

但是我遇到了另一个问题:

org.springframework.beans.NotWritablePropertyException: Invalid property 'sessionFactory' of bean class [org.springframework.orm.jpa.JpaTransactionManager]: Bean property 'sessionFactory' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:2)

你的注释中有一个拼写错误&#34; tansactionManager&#34;缺少&#39; r&#39;,&#34; transactionManager&#34;。我做了更正,对我来说效果很好。

答案 1 :(得分:1)

如果您正在使用会话工厂,那么这应该可以正常工作

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

但是如果你想使用JPA EntityManager,那么你需要

    <bean name="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
             <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>

请参阅migrating-to-spring-3-1-and-hibernate-4-1它包含所需配置的好例子