Question

[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.

Exception:

SEVERE: Exception sending context initialized event to listener
instance of class
org.springframework.web.context.ContextLoaderListener
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;

Dispatcher-servlet.xml

 <!-- 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">
        <value>/WEB-INF/jsp/</value>
      </property>
      <property name="suffix">
        <value>.jsp</value>
      </property>
 </bean>

 <!-- Database properties configuration - START -->
 <bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" 
    p:location="WEB-INF/classes/properties/database.properties">
 </bean>
 <!-- Database properties configuration - END -->

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

<!-- Hibernate Settings - START -->
    <bean id="sessionFactory"
       class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
       <property name="dataSource" ref="dataSource"></property>
       <property name="configLocation">
           <value>classpath:hibernate.cfg.xml</value>
       </property>
       <property name="configurationClass">
           <value>org.hibernate.cfg.AnnotationConfiguration</value>
       </property>
       <property name="hibernateProperties">
           <props>
               <prop key="hibernate.dialect">${jdbc.dialect}</prop>
               <prop key="hibernate.show_sql">true</prop>
           </props>
       </property>
   </bean>
<!-- Hibernate Settings - END -->

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

 <mvc:annotation-driven/>

 <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"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"></property>
</bean>

hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <mapping class="com.inspireme.dto.UserDTO"></mapping>
    </session-factory>
</hibernate-configuration>

Controller class

package com.inspireme.controller;

@Controller
@RequestMapping("/")
public class InspireMeController {

    public ApplicationContext applicationContext;

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

    @Autowired
    private UserBO userBO;

Service class:

package com.inspireme.boimpl;

@Service
public class UserBOImpl implements UserBO {

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

    @Autowired
    private UserDAO userDAO;

Repository class

package com.inspireme.daoimpl;

@Repository
public class UserDAOImpl implements UserDAO {

    @Autowired
    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 .. howtodoinjava.com/2013/12/08/…. But after adding this I am getting an error, “WEB-INF/inspireme-dispatcher-servlet.xml]: Invocation of init method failed; nested exception is java.io.FileNotFoundException: 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