无法将hibernate.cfg.xml解析为URL,因为它不存在。Spring MVC Hibernate web应用程序错误

无法将hibernate.cfg.xml解析为URL,因为它不存在。Spring MVC Hibernate web应用程序错误,spring,hibernate,spring-mvc,hibernate-4.x,hibernate.cfg.xml,Spring,Hibernate,Spring Mvc,Hibernate 4.x,Hibernate.cfg.xml,我遵循这个教程 使用SpringMVC和Hibernate以及MySQL为Web应用构建EclipseMaven项目。使用ApacheTomcat8作为服务器 我花了好几个小时试图解决这个问题,但我被困住了 请帮忙,谢谢。我得到了这个错误 错误:[已编辑] org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'studentController': Injection of

我遵循这个教程

使用SpringMVC和Hibernate以及MySQL为Web应用构建EclipseMaven项目。使用ApacheTomcat8作为服务器

我花了好几个小时试图解决这个问题,但我被困住了

请帮忙,谢谢。我得到了这个错误

错误:[已编辑]

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'studentController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.bgolubovic.service.StudentService com.bgolubovic.controller.StudentController.studentService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'studentServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.bgolubovic.dao.StudentDao com.bgolubovic.service.impl.StudentServiceImpl.studentDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'studentDaoImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory com.bgolubovic.dao.impl.StudentDaoImpl.session; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/spring-servlet.xml]: Invocation of init method failed; nested exception is java.io.FileNotFoundException: class path resource [hibernate.cfg.xml] cannot be resolved to URL because it does not exist
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
spring-servlet.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="com.bgolubovic" />    


<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="annotatedClasses">
    <list>
        <value>com.bgolubovic.model.Student</value>
    </list>
    </property>
    <property name="configLocation">
        <value>classpath:hibernate.cfg.xml</value>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${jdbc.dialect}</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <property name="suffix" value=".jsp"/>
</bean>
<tx:annotation-driven />
<bean id="transactionManager"
    class="org.springframework.orm.hibernate4.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean> 
hibernate.cfg.xml

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

<hibernate-configuration>
<session-factory>        
    <mapping class="com.bgolubovic.model.Student" />
</session-factory>         
</hibernate-configuration>

看起来,您的spring orm-.jar已损坏或与现有spring jar不兼容,或者同一jar有多个冲突版本。检查这些并更正依赖项以解决问题。

您正试图使用Hibernate 4中的org.springframework.orm.hibernate4.LocalSessionFactoryBean,但是(大部分)安装了Hibernate 3依赖项。将您的版本更新为4应该可以解决这个问题-只需检查pom.xml并将其全部更改即可

e、 g


org.hibernate
冬眠核心
4.3.7.最终版本


etc似乎是JAR版本问题


您正试图在
springservlet.xml
中使用
hibernate4


在pom中,当我在pom.xml中更改Hibernate 4->3时,您引用了
版本3

,我得到以下结果:创建名为“sessionFactory”的bean时出错,该bean在ServletContext资源[/WEB-INF/spring servlet.xml]中定义:设置属性值时出错;嵌套异常为org.springframework.beans.NotWritablePropertyException:bean类[org.springframework.orm.hibernate3.LocalSessionFactoryBean]的无效属性'AnnotatedClass':bean属性'AnnotatedClass'不可写或具有无效的setter方法。setter的参数类型是否与getter的返回类型匹配?为什么不在pom中也使用hibernate 4,而不是3?我也改变了这一点,并在所有地方尝试4。我还需要将Spring更改为4.0.x版本。我现在得到了这个错误:创建名为“sessionFactory”的bean时出错,该bean在ServletContext资源[/WEB-INF/SpringServlet.xml]中定义:调用init方法失败;嵌套异常为java.io.FileNotFoundException:无法将类路径资源[hibernate.cfg.xml]解析为URL,因为它不存在org.springframework.beans.factory.annotation.AutoWiredNotationBeanPostProcessor.PostProcessPropertyValue(AutoWiredNotationBeanPostProcessor.java:292)我将其更改为:org.hibernate hibernate entitymanager 4.3.7.Final org.hibernate hibernate core 4.3.7.Final,但仍然是相同的错误。@bgolobovic确保您已更新了所有旧的依赖项,并在必要时强制Maven更新以拉入所有新的JAR。我将spring依赖项更改为4.0.x版本,现在得到这个错误:在ServletContext资源[/WEB-INF/SpringServlet.xml]中定义了名为“sessionFactory”的bean时出错:初始化方法调用失败;嵌套异常为java.io.FileNotFoundException:无法将类路径资源[hibernate.cfg.xml]解析为URL,因为它不存在org.springframework.beans.factory.annotation.AutoWiredNotationBeanPostProcessor.PostProcessPropertyValue(AutoWiredNotationBeanPostProcessor.java:292),如您所见,我已经有了hibernate.cfg.xml,它在WEB-INF目录中,我知道:(位于src\main\webapp\WEB-INF中。感谢您的评论和宝贵时间。
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>CRUDWebAppMavenized</display-name>

<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>classpath:log4j.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

<servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.dialect=org.hibernate.dialect.MySQLDialect
jdbc.databaseurl=jdbc:mysql://localhost:3306/studentdb
jdbc.username=root
jdbc.password=******
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 

<hibernate-configuration>
<session-factory>        
    <mapping class="com.bgolubovic.model.Student" />
</session-factory>         
</hibernate-configuration>