Spring &引用;创建bean时出错";春暖花开
我正在进行一个SpringwithHibernate项目,该项目也使用Maven。我试图使一个网页,有一个表单来捕获搜索信息以及显示结果。我已经创建了dispatcher servlet、web.xml、controller、model和view,但当我尝试在服务器上运行项目时,出现以下500个错误: Servlet dispatcher的Servlet.init()引发异常(stacktrace和下面的文件) 我有点困惑,因为我正在遵循一个教程,我相信我所有的依赖项都是正确的。我已经查看了堆栈跟踪,从我所读到的内容来看,似乎存在版本控制问题。我曾尝试在Spring &引用;创建bean时出错";春暖花开,spring,hibernate,maven,beancreationexception,Spring,Hibernate,Maven,Beancreationexception,我正在进行一个SpringwithHibernate项目,该项目也使用Maven。我试图使一个网页,有一个表单来捕获搜索信息以及显示结果。我已经创建了dispatcher servlet、web.xml、controller、model和view,但当我尝试在服务器上运行项目时,出现以下500个错误: Servlet dispatcher的Servlet.init()引发异常(stacktrace和下面的文件) 我有点困惑,因为我正在遵循一个教程,我相信我所有的依赖项都是正确的。我已经查看了堆栈
pom.xml
文件中更改我的版本,但没有成功。我还检查了WEB-INF/lib
文件夹,我有hibernate核心以及其他必需的JAR,因此我认为pom.xml
配置正确
从我对Spring的理解来看,DispatcherServlet应该注入SessionFactorybean,但它似乎工作不正常。我想如果我能克服sessionFactory的问题,我就能继续前进
pom.xml(我不太确定我的依赖关系是否正确。)
相关原因:
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依赖项。 尝试以下方法(将正确的版本标记添加到依赖项):
org.springframework
spring上下文
org.springframework
春季aop
org.springframework
弹簧式
org.springframework
弹簧芯
org.springframework
春豆
org.springframework
SpringWebMVC
org.springframework
德克萨斯州春季
org.springframework
春季甲虫
org.springframework
弹簧试验
测试
请阅读-总结是,这不是向志愿者讲话的理想方式,可能会对获得答案产生反作用。请不要把这个添加到你的问题中。工作很有魅力!你是个救生员!
<?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>
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";
}
}
<%@ 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>
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
<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>