簡體   English   中英

使用Hibernate配置Spring。創建名稱為'usersDao'的bean時出錯:通過字段'sessionFactory'表示的不滿意依賴性

[英]Configuting Spring with Hibernate.Error creating bean with name 'usersDao': Unsatisfied dependency expressed through field 'sessionFactory'

我正在嘗試使用Hibernate 5配置Spring4。做了一個谷歌研究,但沒有幫助。 我正在使用JUnit測試啟動我的應用程序,只是為了查看Hibernate是否可以正常工作。

Error creating bean with name 'usersDao': Unsatisfied dependency expressed through field 'sessionFactory': Error creating bean with name 'sessionFactory' defined in class path resource [ConfigTest/datasource.xml]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: org.hibernate.cfg.Configuration.<init>(Lorg/hibernate/boot/MetadataSources;)V; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [ConfigTest/datasource.xml]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: org.hibernate.cfg.Configuration.<init>(Lorg/hibernate/boot/MetadataSources;)V

這是我的datasource.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"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

    <context:component-scan base-package="/WebMVCtest/test/Config">
    </context:component-scan>

    <beans profile="dev">
        <context:property-placeholder
            location="ConfigTest/jdbc.properties" />

        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
            destroy-method="close">

            <property name="driverClassName" value="${jdbc.driver}"></property>
            <property name="url" value="${jdbc.url}"></property>
            <property name="password" value="${jdbc.password}"></property>
            <property name="username" value="${jdbc.username}"></property>
        </bean>

        <bean id="sessionFactory"
            class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
            <property name="dataSource" ref="dataSource"></property>
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>

                </props>
            </property>
            <property name="packagesToScan">
                <list>
                    <value>DAO</value>
                </list>
            </property>
        </bean>

    </beans>

</beans>

另外,當我使用Hibernate 4時,我也會遇到錯誤:

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'offersDao': Injection of autowired dependencies failed; nested exception is java.lang.NoClassDefFoundError: org/hibernate/cache/spi/RegionFactory

但我確實在該地址設置了RegionFactory: 在此處輸入圖片描述

提前致謝!

看來packagesToScan屬性需要一個數組

就您而言,我想它看起來像這樣...

<property name="packagesToScan">
    <array>
        <value>DAO</value>
    </array>
</property>

也許只是...

<property name="packagesToScan" value="DAO" />

問題在於版本的兼容性。 所以我使用了Spring 4.0.3.RELEASE和Hibernate 4.3.5.Final,錯誤消失了。

我也有一個錯誤:找不到當前會話。 因此,我對事務bean進行了修改:

<bean id="transactionManager"
            class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
            <property name="dataSource" ref="dataSource"></property>
        </bean>
        <tx:annotation-driven />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM