"创建bean"时出错在Hibernate中使用Spring

时间:2017-12-14 20:38:13

标签: spring hibernate maven beancreationexception

我正在使用一个也使用Maven的Spring with Hibernate项目。我正在尝试创建一个页面,其中包含一个表单来捕获搜索信息以及显示结果。我已经创建了dispatcher-servlet,web.xml,控制器,模型和视图,但是当我尝试在服务器上运行项目时,我收到以下500错误:

  

Servlet调度程序的Servlet.init()抛出异常(下面的堆栈跟踪和文件)。

我有点困惑,因为我正在关注一个教程,我相信我的所有依赖都是正确的。我已经看过堆栈跟踪,从我读过的内容看来,似乎存在版本控制问题。我尝试在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

1 个答案:

答案 0 :(得分:1)

您错过了一些Spring依赖项。 尝试使用这些(将正确的版本标记添加到依赖项):

<%= form_with(model: @user, local: true) do |f| %>
    <div>
        <%= f.label :name %>
        <%= f.text_field :name %>
    </div>

    <div>
        <%= f.label :email %>
        <%= f.email_field :email %>
    </div>
    <%= f.submit %>
<% end %>