【问题标题】:org.springframework.beans.factory.BeanCreationException in a MVC webapp using Spring and Hibernateorg.springframework.beans.factory.BeanCreationException 在使用 Spring 和 Hibernate 的 MVC webapp 中
【发布时间】:2012-06-04 05:39:45
【问题描述】:

自 2 天以来,我一直在尝试自行解决问题,但无法找出解决方案...我尝试参考这两个问题,但没有帮助。我错过了一些东西: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'sessionFactory' Spring MVC and Hibernate configurations

我按照本教程使用 Maven 创建了一个简单的 Spring MVC + Hibernate Web 应用程序:http://viralpatel.net/blogs/2010/11/spring3-mvc-hibernate-maven-tutorial-eclipse-example.html 我使用 maven 或使用 pom.xml 导入库没有任何问题。我可以正确部署我使用 Maven 创建的战争。

编辑: 正如我的问题的答案所指出的,我没有声明 commons-collections 依赖项。添加后,这是我得到的异常:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'contactController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private net.viralpatel.contact.service.ContactService net.viralpatel.contact.controller.ContactController.contactService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'contactServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private net.viralpatel.contact.dao.ContactDAO net.viralpatel.contact.service.ContactServiceImpl.contactDAO; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'contactDAOImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory net.viralpatel.contact.dao.ContactDAOImpl.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/spring-servlet.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'configurationClass' of bean class [org.springframework.orm.hibernate4.LocalSessionFactoryBean]: Bean property 'configurationClass' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
    org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)

注意:如果我删除以下属性:

<property name="configurationClass">
            <value>org.hibernate.cfg.AnnotationConfiguration</value>

然后我在tomcat中部署应用后出现404错误。

这里是现在的 servlet-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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:lang="http://www.springframework.org/schema/lang"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

    <context:annotation-config />
    <context:component-scan base-package="net.viralpatel.contact" />

    <bean id="jspViewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>

    <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:messages" />
        <property name="defaultEncoding" value="UTF-8" />
    </bean>
    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
        p:location="/WEB-INF/jdbc.properties" />

    <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 id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <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>

    <tx:annotation-driven />

    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>


</beans>

【问题讨论】:

  • 会话工厂属性中不需要configurationClass
  • 在一个句子中混用 spring、hibernate 和 simple 等词是你的第一个错误。尝试使用 grails 来简化。
  • 如果我取消配置类,maven 正在正确构建战争,tomcat 中的部署正常,日志中没有任何内容,但是当我尝试访问应用程序时,出现 404 错误。我再次检查了配置类,我最终再次获得了 bean 类 [org.springframework.orm.hibernate4.LocalSessionFactoryBean] 的无效属性“configurationClass”:Bean 属性“configurationClass”不可写或具有无效的 setter 方法. setter的参数类型和getter的返回类型是否匹配?
  • @dukable 与同一个例子完全相同的问题。如果您找到任何解决方案,请告诉我。

标签: java spring hibernate model-view-controller


【解决方案1】:

java.lang.NoClassDefFoundError: org/apache/commons/collections/CursorableLinkedList

commons-collection.jar 添加到你的类路径

HibernateTransactionManager中没有这样的属性,删除即可

<property name="configurationClass">
            <value>org.hibernate.cfg.AnnotationConfiguration</value>
</property>

【讨论】:

  • 谢谢,我摆脱了这个异常,但是我仍然在下面的评论中提到一个异常,说 configurationClass 不可写。如果我按照建议将其取下,那么在 tomcat (404) 中部署后我将无法访问我的网络应用程序。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-02
  • 1970-01-01
  • 2015-06-24
  • 2014-03-16
  • 1970-01-01
  • 2015-08-28
相关资源
最近更新 更多