[Solved] Could not autowire field: Spring-Hibernate

Following is the exception which I am getting while I am trying to integrate Hibernate with my Spring MVC project and add Autowired feature.


SEVERE: Exception sending context initialized event to listener
instance of class
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name ‘userBOImpl’: Injection of autowired
dependencies fai led; nested exception is
org.springframework.beans.factory.BeanCreationException: Could not
autowire field: private com.inspireme.dao.UserDA O
com.inspireme.boimpl.UserBOImpl.userDAO; nested exception is
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name ‘userDAOImpl’: Injection of autowired
dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationExcep tion: Could not
autowire field: private org.hibernate.SessionFactory
com.inspireme.daoimpl.UserDAOImpl.sessionFactory; nested exception is
j ava.lang.NoClassDefFoundError: Lorg/hibernate/cache/CacheProvider;


 <!-- Enable Annotation based configuration -->
 <context:annotation-config />

 <!-- Base package -->
 <context:component-scan base-package="com.inspireme." />

 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix">
      <property name="suffix">

 <!-- Database properties configuration - START -->
 <bean id="propertyConfigurer"
 <!-- Database properties configuration - END -->

 <!-- Accessing DB with available credentials - START -->
 <bean id="dataSource"
    class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"
    p:url="${jdbc.databaseurl}" p:username="${jdbc.username}"
 <!-- Accessing DB with available credentials - END -->

<!-- Hibernate Settings - START -->
    <bean id="sessionFactory"
       <property name="dataSource" ref="dataSource"></property>
       <property name="configLocation">
       <property name="configurationClass">
       <property name="hibernateProperties">
               <prop key="hibernate.dialect">${jdbc.dialect}</prop>
               <prop key="hibernate.show_sql">true</prop>
<!-- Hibernate Settings - END -->

 <mvc:resources mapping="/resources/**" location="/resources/inspiremetheme/" />


 <tx:annotation-driven />

 <!-- Start of Autowired beans -->
 <bean id="userBO" class="com.inspireme.boimpl.UserBOImpl"></bean>
 <bean id="userDAO" class="com.inspireme.daoimpl.UserDAOImpl"></bean>
 <!-- End of Autowired beans -->

 <bean id="transactionManager"
    <property name="sessionFactory" ref="sessionFactory"></property>


<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"

        <mapping class="com.inspireme.dto.UserDTO"></mapping>

Controller class

package com.inspireme.controller;

public class InspireMeController {

    public ApplicationContext applicationContext;

    private static final Logger LOGGER = Logger.getLogger(InspireMeController.class);

    private UserBO userBO;

Service class:

package com.inspireme.boimpl;

public class UserBOImpl implements UserBO {

    private static final Logger LOGGER = Logger.getLogger(UserBOImpl.class);

    private UserDAO userDAO;

Repository class

package com.inspireme.daoimpl;

public class UserDAOImpl implements UserDAO {

    private SessionFactory sessionFactory;

Why am I getting this exception since all the 3 beans, are defined in the dispatcher-servlet.xml file.

I am referring the following source to integrate Hibernate.

Enquirer: Amarnath


Solution #1:

java.lang.NoClassDefFoundError: Lorg/hibernate/cache/CacheProvider jar is missing.

You are partially correct .. there is a jar mismatch .. as stated here ..…. But after adding this I am getting an error, “WEB-INF/inspireme-dispatcher-servlet.xml]: Invocation of init method failed; nested exception is class path esource [hibernate.cfg.xml] cannot be resolved to URL because it does not exist”. Please look at my folder structure and let me know how to give the path here.

Not exactly sure how the build works in your case but try classpath:/hibernate/hibernate.cfg.xml or classpath:hibernate/hibernate.cfg.xml. Or better check where exactly that file is within WEB-INF/classes in the deployed war.

Respondent: Amarnath

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy