【问题标题】:Spring error: No qualifying bean of type [org.hibernate.SessionFactory] found for dependencySpring错误:没有为依赖找到[org.hibernate.SessionFactory]类型的合格bean
【发布时间】:2016-05-13 16:00:46
【问题描述】:

让我先发帖full stack trace

基本上,我有这个 DAO 类:

package nl.alli.persistence.util;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by thijm on 13-5-2016.
 */
@Component
public class Dao {
    @Autowired
    private SessionFactory sessionFactory;

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

我正在尝试结合使用 spring 和 hibernate 5 来自动装配 SessionFactory。

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

    <!--&lt;!&ndash; Register Annotation-based Post Processing Beans &ndash;&gt;-->
    <!--<context:annotation-config />-->

    <!--&lt;!&ndash; Scan context package for any eligible annotation configured beans. &ndash;&gt;-->
    <!--<context:component-scan base-package="nl.alli" />-->
    <context:annotation-config/>
    <bean id="myDataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://url.url"/>
        <property name="password" value="tdjfkladsf"/>
        <property name="username" value="jaskdf"/>
    </bean>

    <bean id="mySessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="hibernateProperties">
            <value>
                hibernate.dialect=org.hibernate.dialect.MySQLDialect
            </value>
        </property>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" value="mySessionFactory"/>
    </bean>



    <!--<bean id="BjornJansonDataCollectorBean" class="nl.alli.pvoutput.BjornJansonDataCollector"/>-->
    <!--<bean id="PVOutputDataCollector" class="nl.alli.pvoutput.PVoutputDataCollector"/>-->
    <!--<bean id="PVOutputDataServiceImplBean" class="nl.alli.persistence.PVOutputDataServiceImpl"/>-->
    <!--<bean id="PVOutputDataDaoImplBean" class="nl.alli.persistence.PVOutputDataDaoImpl"/>-->
    <!--<bean id="DaoBean" class="nl.alli.persistence.util.Dao"/>-->
</beans>

甚至 IntelliJ 也能看到 private SessionFactory sessionFactoryspring.xml 中的 bean 之间的链接。我不知道是什么导致了异常,如果有人可以帮助我,那就太好了。

提前致谢!

【问题讨论】:

  • 可能spring.xml 从未被拾取,因此mySessionFactory 尚未被初始化,如下面的答案所示。如果你打开休眠日志,你可以很容易地弄清楚。至少,在启动过程中它应该打印出休眠配置。

标签: java spring hibernate spring-boot


【解决方案1】:

这个错误的发生基本上是因为 Spring bean 没有从你的 Spring XML 文件中创建,这意味着你的 spring.xml 没有被加载到内存中。

以下是解决您问题的一些提示:

  1. 检查是否从 web.xml 正确加载 spring.xml

  2. 理想情况下,您应该将所有 bean 从 spring.xml 文件移动到 applicationContext.xml 文件

【讨论】:

    【解决方案2】:

    我看到你使用 spring-boot。据我所知,默认情况下它没有获取 xml 配置。

    您是否已将 xml 导入到类似代码中某处的 java 配置中

    @ImportResource("classpath:spring.xml")
    

    【讨论】:

    • 这样做会给我以下错误:java.io.FileNotFoundException: class path resource [spring.xml] cannot be opened because it does not exist
    • 在构建过程中它位于何处?你的 pom.xml 中有什么?
    【解决方案3】:

    看起来您的类路径中缺少库,如果您使用的是 Maven,请添加如下内容:

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-19
      • 2015-06-15
      • 2015-01-29
      • 2016-12-31
      • 2018-12-02
      • 2016-12-27
      • 1970-01-01
      相关资源
      最近更新 更多