注入自动连接的依赖项失败;嵌套异常是org.springframework.beans.factory.BeanCreationException:sessionFactory

时间:2014-03-24 14:18:08

标签: spring spring-mvc spring-security

我收到以下错误

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'roleDAOImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory com.quad.dao.RoleDAOImpl.sessionFactory; nested exception is java.lang.NoClassDefFoundError: Lorg/hibernate/cache/CacheProvider;

我在mvc-dispatcher-servlet.xml中的sessionFactory配置是

<bean id="sessionFactory class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"><property name="dataSource" ref="dataSource" />    <property name="hibernateProperties">    <props>    <prop key="hibernate.dialect">${hibernate.dialect}</prop>    <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>    <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>    </props>    </property>    </bean>  

我在RoleDAOImpl中使用sessionFactory

我的完整mvc-dispatcher-servlet.xml是

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans     
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:component-scan base-package="com.quad.controller" />
<context:component-scan base-package="com.quad.dao" />
<context:component-scan base-package="com.quad.entity" />
<context:component-scan base-package="com.quad.service" />

<context:property-placeholder location="classpath:database.properties" />
<!-- <context:property-placeholder location="classpath:spring-security.xml" />  --> 

<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>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
      <property name="driverClassName" value="${database.driver}" />  
      <property name="url" value="${database.url}" />  
      <property name="username" value="${database.user}" />  
      <property name="password" value="${database.password}" />  
 </bean>  

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">  
      <property name="dataSource" ref="dataSource" />  
      <property name="hibernateProperties">  
       <props>  
        <prop key="hibernate.dialect">${hibernate.dialect}</prop>  
        <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>  
        <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>      
       </props>  
      </property>  
</bean>  

请帮忙。

1 个答案:

答案 0 :(得分:1)

这肯定是依赖版本不匹配的问题。

在Hibernate 4中删除了

org.hibernate.cache.CacheProvider(创建了一个不同的缓存标准)

您指定的会话工厂类是: org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean

特定于Hibernate 3。

这意味着您正在使用与错误的会话工厂类相关的hibernate 4相关依赖项。

只要你使用Spring 3.1.0.RELEASE或更高版本,就可以在spring-orm中使用hibernate 4类(和hibernate3版本一样)。

您有两种选择:

  1. 更改回Hibernate 3
    • 这取决于项目的构建方式。
    • 如果它正在使用Maven,这将涉及更改hibernate-core依赖项
  2. 将弹簧配置更改为Hibernate 4
    • 这仅适用于Spring 3.1.0.RELEASE或更高版本
    • 对于此更改,上述AnnotationSessionFactoryBean为:
      org.springframework.orm.hibernate4.LocalSessionFactoryBean