java - 使用 Hibernate 配置 Spring。创建名称为 'usersDao' : Unsatisfied dependency expressed through field 'sessionFactory' 的 bean 时出错

标签 java spring hibernate spring-mvc

我正在尝试使用 Hibernate 5 配置 Spring 4。进行了谷歌研究,但没有帮助。我正在使用 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: enter image description here

提前致谢!

最佳答案

appears that the packagesToScan property wants an array .

就你的情况而言,我想它看起来像这样......

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

或者也许只是......

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

关于java - 使用 Hibernate 配置 Spring。创建名称为 'usersDao' : Unsatisfied dependency expressed through field 'sessionFactory' 的 bean 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40566070/

相关文章:

java - 如何将 JSONObject 传递到 PHP 文件?

java - 如何将数组列表转换为多维数组

java - 如何在java dsl中设置消息驱动的入站适配器的恢复间隔?

java - 我需要在 hibernate 中的多态查询中查询两次吗?

java - 什么是NullPointerException,我该如何解决?

java - 双括号集合初始值设定项中的未定义构造函数错误

java - Spring bean配置列表<map>

spring - 如何使用 Spring Data JPA 根据 bool 参数包含或排除记录?

java - 在 Hibernate 中使用接口(interface)而不是具体类映射 HashMap

hibernate - 什么时候在Hibernate中使用@Version和@Audited?