"Error creating bean" 在休眠中 Spring

"Error creating bean" in Hibernate with Spring

我正在使用 Hibernate 项目开发 Spring,该项目也使用 Maven。我正在尝试制作一个页面,该页面具有用于捕获搜索信息并显示结果的表单。我已经创建了调度程序 servlet、web.xml、控制器、模型和视图,但是当我尝试 运行 服务器上的项目时,我收到以下 500 错误:

Servlet.init() for servlet dispatcher threw exception (stacktrace and files below).

我有点困惑,因为我正在学习教程并且我相信我的所有依赖项都是正确的。我查看了堆栈跟踪,从我读到的内容来看,似乎存在版本控制问题。我曾尝试更改 pom.xml 文件中的版本,但没有成功。我还检查了 WEB-INF/lib 文件夹,我有 hibernate-core 以及其他所需的 jar,所以我认为 pom.xml 配置正确。

根据我对 Spring 的了解,dispatcher-servlet 应该注入 sessionFactory bean,但它似乎无法正常工作。我想如果我能解决 sessionFactory 问题,我就能继续前进。

pom.xml(我不太确定我的依赖是否正确。)

    <project xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
    http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.yccd</groupId>
        <artifactId>StudentDataXML</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>war</packaging>
    <properties>

   <org.springframework.version>4.3.2.RELEASE</org.springframework.version>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${org.springframework.version}</version>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>3.1.0</version>
            </dependency>
            <dependency>
                <groupId>javax.servlet.jsp</groupId>
                <artifactId>javax.servlet.jsp-api</artifactId>
                <version>2.3.1</version>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jstl</artifactId>
                <version>1.2</version>
            </dependency>
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-core</artifactId>
                <version>5.1.0.Final</version>
            </dependency>
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-validator</artifactId>
                <version>5.2.4.Final</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-tx</artifactId>
                <version>${org.springframework.version}</version>
            </dependency>

            <!-- <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc</artifactId>
        <version>10.2.0</version>
        </dependency> -->
        <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>${org.springframework.version}</version>
        </dependency>

        </dependencies>
        <build>
            <resources>
                <resource>
                    <directory>src/main/resources</directory>
                    <excludes>
                        <exclude>**/*.java</exclude>
                    </excludes>
                </resource>
                <resource>
                    <directory>src/main/webapp</directory>
                    <excludes>
                        <exclude>**/*.java</exclude>
                    </excludes>
                </resource>
            </resources>
            <plugins>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.6.1</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.0.0</version>
                </plugin>
            </plugins>
        </build>
    </project>

调度员-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:tx="http://www.springframework.org/schema/tx"     
    xmlns:context="http://www.springframework.org/schema/context"
     xmlns:util="http://www.springframework.org/schema/util"     
    xmlns:mvc="http://www.springframework.org/schema/mvc"
     xsi:schemaLocation="http://www.springframework.org/schema/mvc     
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
      http://www.springframework.org/schema/beans     
    http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/util     
    http://www.springframework.org/schema/util/spring-util.xsd
      http://www.springframework.org/schema/context     
    http://www.springframework.org/schema/context/spring-context.xsd
         http://www.springframework.org/schema/tx     
    http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/aop     
    http://www.springframework.org/schema/aop/spring-aop.xsd">


        <context:component-scan base-package="com.yccd.controllers" />
        <context:component-scan base-package="com.yccd.model" />

        <mvc:annotation-driven />

        <bean

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

    <context:property-placeholder location="classpath:connection.properties" />
        <bean name="dataSource"
            class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="${jdbc.driverClass}" />
            <property name="url" value="${jdbc.url}" />
            <property name="username" value="${jdbc.username}" />
            <property name="password" value="${jdbc.password}" />
        </bean>
        <bean id="sessionFactory"  
      class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
      <property name="dataSource" ref="dataSource" />
      <property name="annotatedClasses">
       <list>
        <value>com.yccd.model.User</value>    
       </list>
      </property>
      <property name="hibernateProperties">
       <props>
        <prop key="hibernate.dialect">${jdbc.dialect}</prop>
         <prop key="hibernate.hbm2ddl.auto">update</prop>
        <prop key="hibernate.show_sql">true</prop>
       </props>
      </property>
     </bean>
     <tx:annotation-driven />
        <bean id="transactionManager"
      class="org.springframework.orm.hibernate5.HibernateTransactionManager">
      <property name="sessionFactory" ref="sessionFactory" />
     </bean>
    </beans>

StudentController.java(我打算用这个作为应用的主控制器)

package com.yccd.controllers;

import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.yccd.model.Student;

@Controller
@RequestMapping(value = "/student")
public class StudentController {

@RequestMapping(value = "/showAll", method = RequestMethod.GET)
public String sayHelloAgain(@PathVariable("queryType") String queryType,     
ModelMap model) {
        model.addAttribute("message", "Just to say hello again");
        model.addAttribute("queryTypeChoice", queryType);
        return "welcomeAgain";
    }

    @RequestMapping(value = "/searchResults", method = RequestMethod.GET)
    public String displayStudents(ModelMap model) {
        return "displayStudentRecords";
    }

    @RequestMapping(value = "/search", method = RequestMethod.POST)
    public String search(Student student, Map<String, Object> map) {
        //TO-DO: GET PARAM VALUES AND MAKE A CALL TO SERVICE LAYER HERE
        return "displayStudentRecords";
    }
}

displayStudentRecords.jsp(这是我要显示从数据库中获取记录的页面)

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>YCCD - Search</title>
</head>
<body>
<h3>Search Student Record Form</h3>
    <form name="searchStudentsForm" action="/search/searchStudents">
        <table>
            <tr>
                <td>Student ID:</td>
                <td><input type="text" name="stid" /></td>
            </tr>
            <tr>
                <td>Gender:</td>
                <td><input type="text" name="gender" /></td>
            </tr>
            <tr>
                <td>AGE:</td>
                <td><input type="text" name="age" /></td>
            </tr>
            <tr>
            <td colspan="2"><input type="submit" value="Search"></td>
            </tr>
        </table>
    </form>
</body>
</html>

我对 Spring、Hibernate 和 Maven 还很陌生。如果这看起来微不足道,我深表歉意。

这只是堆栈跟踪的一部分。我不太擅长通过查看堆栈跟踪来确定问题出在哪里,所以如果有一些指示可以给我,比如首先要查看它的哪一部分,或者是否有任何区域我可以忽略,这也会很有帮助。

堆栈跟踪

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping': Invocation of init method failed; nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.orm.hibernate5.LocalSessionFactoryBean] for bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]; nested exception is java.lang.ClassNotFoundException: org.springframework.orm.hibernate5.LocalSessionFactoryBean

相关原因:

org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.orm.hibernate5.LocalSessionFactoryBean] for bean with name 'sessionFactory' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]; nested exception is java.lang.ClassNotFoundException: org.springframework.orm.hibernate5.LocalSessionFactoryBean

您缺少一些 spring 依赖项。 尝试使用这些(将正确的版本标记添加到依赖项):

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aop</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring-expression</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <scope>test</scope>
    </dependency>