Joel Christ
Joel Christ

Reputation: 31

Spring error: No qualifying bean of type [org.hibernate.SessionFactory] found for dependency

Let me start off by posting the full stack trace .

Basicly, I have this DAO class:

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;
    }
}

I'm trying to use spring in combination with hibernate 5 to autowire the SessionFactory.

My spring.xml looks like this:

<?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>

Even IntelliJ sees the link between the private SessionFactory sessionFactory and the bean in spring.xml. I have no idea what is causing the exception, if anyone can help me out that'd be great.

Thanks in advance!

Upvotes: 0

Views: 945

Answers (3)

Simeon Leyzerzon
Simeon Leyzerzon

Reputation: 19074

Looks like you are missing libraries on your classpath, if you are using Maven, add something like this:

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

Upvotes: 0

inigo skimmer
inigo skimmer

Reputation: 918

I see that you use spring-boot. As far as I know it didn't get xml configs by default.

Have you imported xml to java configuration somewhere in you code like this

@ImportResource("classpath:spring.xml")

Upvotes: 1

shankarsh15
shankarsh15

Reputation: 1967

This error is happening basically due to the fact that Spring beans are not getting created from your Spring XML file meaning your spring.xml is not getting loaded in memory .

Here are some pointers to resolve your issue:

  1. Check if you are loading spring.xml correctly from your web.xml

  2. Ideally you should move all your beans from spring.xml file to applicationContext.xml file

Upvotes: 1

Related Questions