springMvc hibernate 搭建出错

问题描述

org.springframework.beans.factory.BeanCreationException:Errorcreatingbeanwithname'studentController':Injectionofautowireddependenciesfailed;nestedexceptionisorg.springframework.beans.factory.BeanCreationException:Couldnotautowirefield:publiccom.mvc.service.StudentServicecom.mvc.controller.StudentController.studentService;nestedexceptionisorg.springframework.beans.factory.NoSuchBeanDefinitionException:Nomatchingbeanoftype[com.mvc.service.StudentService]foundfordependency:expectedatleast1beanwhichqualifiesasautowirecandidateforthisdependency.Dependencyannotations:{@org.springframework.beans.factory.annotation.Autowired(required=true)}atorg.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:286)atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1055)atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:511)atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450)atorg.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:290)atorg.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)atorg.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:287)atorg.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:189)atorg.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:562)atorg.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:871)atorg.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:423)atorg.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:443)atorg.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:459)atorg.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:340)atorg.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:307)atorg.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:127)atjavax.servlet.GenericServlet.init(GenericServlet.java:212)atorg.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1161)atorg.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:806)atorg.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:129)atorg.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)atorg.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)atorg.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)atorg.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)atorg.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)atorg.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)atorg.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)atorg.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)atjava.lang.Thread.run(Thread.java:619)Causedby:org.springframework.beans.factory.BeanCreationException:Couldnotautowirefield:publiccom.mvc.service.StudentServicecom.mvc.controller.StudentController.studentService;nestedexceptionisorg.springframework.beans.factory.NoSuchBeanDefinitionException:Nomatchingbeanoftype[com.mvc.service.StudentService]foundfordependency:expectedatleast1beanwhichqualifiesasautowirecandidateforthisdependency.Dependencyannotations:{@org.springframework.beans.factory.annotation.Autowired(required=true)}atorg.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:507)atorg.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84)atorg.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:283)...28moreCausedby:org.springframework.beans.factory.NoSuchBeanDefinitionException:Nomatchingbeanoftype[com.mvc.service.StudentService]foundfordependency:expectedatleast1beanwhichqualifiesasautowirecandidateforthisdependency.Dependencyannotations:{@org.springframework.beans.factory.annotation.Autowired(required=true)}atorg.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:901)atorg.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:770)atorg.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:685)atorg.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)...30more2015-2-289:45:58org.apache.catalina.core.StandardWrapperValveinvoke严重:Allocateexceptionforservletspringorg.springframework.beans.factory.NoSuchBeanDefinitionException:Nomatchingbeanoftype[com.mvc.service.StudentService]foundfordependency:expectedatleast1beanwhichqualifiesasautowirecandidateforthisdependency.Dependencyannotations:{@org.springframework.beans.factory.annotation.Autowired(required=true)}atorg.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:901)atorg.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:770)atorg.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:685)atorg.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)atorg.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84)atorg.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:283)atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1055)atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:511)atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450)atorg.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:290)atorg.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)atorg.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:287)atorg.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:189)atorg.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:562)atorg.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:871)atorg.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:423)atorg.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:443)atorg.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:459)atorg.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:340)atorg.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:307)atorg.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:127)atjavax.servlet.GenericServlet.init(GenericServlet.java:212)atorg.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1161)atorg.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:806)atorg.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:129)atorg.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)atorg.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)atorg.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)atorg.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)atorg.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)atorg.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)atorg.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)atorg.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)atjava.lang.Thread.run(Thread.java:619)

解决方案

解决方案二:
packagecom.mvc.controller;importjava.util.List;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Controller;importorg.springframework.ui.ModelMap;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestParam;importcom.mvc.entity.Student;importcom.mvc.service.StudentService;importcom.sun.org.apache.commons.logging.Log;importcom.sun.org.apache.commons.logging.LogFactory;@Controller@RequestMapping("/student.do")publicclassStudentController{protectedfinaltransientLoglog=LogFactory.getLog("StudentController.class");@AutowiredpublicStudentServicestudentService;publicStudentController(){}publicStudentServicegetStudentService(){returnstudentService;}publicvoidsetStudentService(StudentServicestudentService){this.studentService=studentService;}@RequestMappingpublicStringload(ModelMapmodelMap){List<Student>list=studentService.findAll();modelMap.put("list",list);return"student";}@RequestMapping(params="method=add")publicStringadd(HttpServletRequestrequest,ModelMapmodelMap)throwsException{return"student_add";}@RequestMapping(params="method=save")publicStringsave(HttpServletRequestrequest,ModelMapmodelMap){Studentst=newStudent();Stringname=request.getParameter("name");Stringpsw=request.getParameter("psw");st.setName(name);st.setPsw(psw);try{studentService.save(st);modelMap.put("addstate","添加成功");}catch(Exceptione){e.printStackTrace();log.error(e.getMessage());modelMap.put("addstate","添加失败");}return"student_add";}@RequestMapping(params="method=del")publicvoiddel(@RequestParam("id")Stringid,HttpServletResponseresponse){try{Studentst=newStudent();st.setId(Integer.valueOf(id));studentService.delete(st);response.getWriter().print("{"del":"true"}");}catch(Exceptione){log.error(e.getMessage());e.printStackTrace();}}}这是controller
解决方案三:
packagecom.mvc.service;importjava.util.List;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;importorg.springframework.transaction.annotation.Transactional;importcom.mvc.dao.StudentDao;importcom.mvc.entity.Student;@Service("studentService")@TransactionalpublicclassStudentService{@AutowiredpublicStudentDaostudentDao;publicvoidsave(Studentst){studentDao.save(st);}publicvoiddelete(Studentobj){studentDao.delete(obj);}publicList<Student>findAll(){returnstudentDao.findAll();}publicvoidupdate(Studentmodel){studentDao.update(model);}}这是Service
解决方案四:
packagecom.mvc.entity;importjava.io.Serializable;importjavax.persistence.Basic;importjavax.persistence.Column;importjavax.persistence.Entity;importjavax.persistence.GeneratedValue;importjavax.persistence.GenerationType;importjavax.persistence.Id;importjavax.persistence.Table;@Entity@Table(name="student")publicclassStudentimplementsSerializable{privatestaticfinallongserialVersionUID=1L;privateIntegerid;privateStringname;privateStringpsw;@Id@Basic(optional=false)@Column(name="id")@GeneratedValue(strategy=GenerationType.IDENTITY)publicIntegergetId(){returnid;}publicvoidsetId(Integerid){this.id=id;}@Column(name="name")publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.name=name;}@Column(name="psw")publicStringgetPsw(){returnpsw;}publicvoidsetPsw(Stringpsw){this.psw=psw;}}这是Entity
解决方案五:
注解配错了...
解决方案六:
引用4楼u010425898的回复:

注解配错了...

那个地方注解错了
解决方案七:
spring配置贴出来看看,你的sessionFactory的class是不是:org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean?
解决方案八:
引用6楼ligang819350766的回复:

spring配置贴出来看看,你的sessionFactory的class是不是:org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean?

<?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd"><context:annotation-config/><!--把标记了@Controller注解的类转换为bean--><context:component-scanbase-package="com.mvc.controller"/><!--启动SpringMVC的注解功能,完成请求和注解POJO的映射--><beanclass="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/><!--对模型视图名称的解析,即在模型视图名称添加前后缀--><beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"p:prefix="/WEB-INF/view/"p:suffix=".jsp"/><beanid="multipartResolver"class="org.springframework.web.multipart.commons.CommonsMultipartResolver"p:defaultEncoding="utf-8"/></beans>
解决方案九:
引用6楼ligang819350766的回复:

spring配置贴出来看看,你的sessionFactory的class是不是:org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean?

<beanid="sessionFactory"class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"><propertyname="dataSource"ref="dataSource"/><propertyname="hibernateProperties"><props><propkey="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop><propkey="hibernate.hbm2ddl.auto">update</prop><propkey="show_sql">true</prop><propkey="format_sql">true</prop></props></property><propertyname="packagesToScan"><list><value>com.mvc.entity</value><!--扫描实体类,也就是平时所说的model--></list></property></bean>
解决方案十:
http://blog.csdn.net/gst6062825/article/details/8765157
解决方案十一:
引用9楼GostJR的回复:

http://blog.csdn.net/gst6062825/article/details/8765157

??????
解决方案十二:
我就讲方法好了:错误注释“Errorcreatingbeanwithname'studentController':Injectionofautowireddependenciesfailed;”一个叫studentController的注入错误。下面一句:Couldnotautowirefield:publiccom.mvc.service.StudentServicecom.mvc.controller.StudentController.studentService;这句不能自动装配下面这个包的studentService下句Nomatchingbeanoftype[com.mvc.service.StudentService]foundfordependency没有找到相匹配的类型。仔细看看是不是注释,配置哪里弄错了,自己弄出来不会忘。
解决方案十三:
引用10楼qq_24366755的回复:

Quote: 引用9楼GostJR的回复:
http://blog.csdn.net/gst6062825/article/details/8765157

??????

你首先要仔仔细细的看清楚你报的错误,就像楼上说的,为什么注入bean的时候会报找不到匹配类型的错误?怀着问题来看我发给你的帖子
解决方案十四:
注解@Service("studentService")和@Transactional应该标在StudentService的实现类类名上
解决方案十五:
异常表明属性没有注入进去注解@Service("studentService")应该写到StudentService的实现类类名上比如@Service@TransactionalpublicclassUserServiceImplextendsDaoSupportImpl<User>implementsUserService{
解决方案:
引用14楼tsshouhu的回复:

异常表明属性没有注入进去注解@Service("studentService")应该写到StudentService的实现类类名上比如@Service@TransactionalpublicclassUserServiceImplextendsDaoSupportImpl<User>implementsUserService{

我是写在实现类上的
解决方案:
引用13楼c306081148的回复:

注解@Service("studentService")和@Transactional应该标在StudentService的实现类类名上

我是写在实现类上的
解决方案:
引用11楼weidea的回复:

我就讲方法好了:错误注释“Errorcreatingbeanwithname'studentController':Injectionofautowireddependenciesfailed;”一个叫studentController的注入错误。下面一句:Couldnotautowirefield:publiccom.mvc.service.StudentServicecom.mvc.controller.StudentController.studentService;这句不能自动装配下面这个包的studentService下句Nomatchingbeanoftype[com.mvc.service.StudentService]foundfordependency没有找到相匹配的类型。仔细看看是不是注释,配置哪里弄错了,自己弄出来不会忘。

我还是刚接触不懂springmvchibernate
解决方案:
你都没有StudentService的接口,怎么面向接口编程,怎么注解啊。
解决方案:
@Controller@RequestMapping("/student.do")publicclassStudentController{protectedfinaltransientLoglog=LogFactory.getLog("StudentController.class");@AutowiredpublicStudentServicestudentService;你自己注意StudentService要求是接口,你现在放的是实现类,那还要什么注入,你都可以自己new了,当然你不能这么做。
解决方案:
引用19楼u014074757的回复:

@Controller@RequestMapping("/student.do")publicclassStudentController{protectedfinaltransientLoglog=LogFactory.getLog("StudentController.class");@AutowiredpublicStudentServicestudentService;你自己注意StudentService要求是接口,你现在放的是实现类,那还要什么注入,你都可以自己new了,当然你不能这么做。

那你的意思是我把StudentService作为接口?
解决方案:
我的意思是你在controller中需要引用的是一个接口,而你却直接引用了实现了,这是不对的。他们的关系应该是这样的:StudentController内部引用一个StudentService(接口),你自己需要写一个StudentService的实现,例如:StudentServiceImplimplementsStudentService。而你现在controller引用了实例。StudentController引用StudentService(接口)<--StudentServiceImpl(实现),注入的是StudentServiceImpl。

时间: 2024-09-27 15:32:51

springMvc hibernate 搭建出错的相关文章

Maven搭建SpringMVC+Hibernate项目详解(转)

  前言      今天复习一下SpringMVC+Hibernate的搭建,本来想着将Spring-Security权限控制框架也映入其中的,但是发现内容太多了,Spring-Security的就留在下一篇吧,这篇主要搭建SpringMVC4.1.4和Hibernate4.3.8,之前也打了好多SpringMVC的,这部分已经非常的熟悉了,毕竟业开发过一年多SpringMVC的,这次持久层采用Hibernate,数据源采用c3p0,数据库暂采用MySQL,主要是想复习一下Hibernate.搭

Spring+SpringMVC+Hibernate 开启事务为何无反应?(已贴代码)

问题描述 问题说明:使用Spring+SpringMVC+Hibernate架构来做个练习的时候,就出错了...按道理说这个问题,只需要配置一个事务就可以了,但是配置了还是出错...求解...===========================================================================新增用户出错=========================================================================

关于springmvc+hibernate的问题

问题描述 使用springmvc+hibernate用注解配置实体类的时候,我不使用hbm了,完全使用注解的方式不过有些实体类具有外键关系,怎么使用注解配置多对多,一对多的关系,真不知道该怎么写,百度上很难找到这类的信息有哪位大神来解释下呗?另外还有一个关于日期传递的问题代码如下实体类:@Entity@Table(name="user")publicUser{intid;//id在数据库中设置为自增了Stringname;Java.util,Datedate;publicUser(){

spring mvc-使用MyEclipse集成SpringMVC+Hibernate运行的时候提示这个错误是为什么啊?

问题描述 使用MyEclipse集成SpringMVC+Hibernate运行的时候提示这个错误是为什么啊? 严重: Exception starting filter struts2Unable to load configuration. - bean - jar:file:/D:/MyEcWorkSpa2015/.metadata/.me_tcat7/webapps/TestSSH/WEB-INF/lib/struts2-core-2.2.1.jar!/struts-default.xml

spring和hibernate整合出错,请大神帮忙解决,谢谢

问题描述 spring和hibernate整合出错,请大神帮忙解决,谢谢 <!-- Hibernate和spring整合的配置 配置SessionFactory --> <!-- 指定hibernate的配置文件位置 --> <!-- 配置c3p0数据库连接池 --> <!--通过反射设置set方法--> <!-- 数据连接信息 --> <!-- 其他配置 --> <!-- 初始化时获取三个连接,取值应在minPoolSize与

hibernate4-第一次使用hibernate就出错

问题描述 第一次使用hibernate就出错 使用hibernate4报错Initial SessionFactory creation failed.java.lang.NullPointerExceptionException in thread ""main"" java.lang.ExceptionInInitializerError at HibernateUtil.buildSessionFactory(HibernateUtil.java:22) at

springmvc+hibernate 实体类映射错误

问题描述 springmvc+hibernate 实体类映射错误 type Exception report message Request processing failed; nested exception is org.hibernate.hql.ast.QuerySyntaxException: Users is not mapped [from Users u where u.loginName='ss' and u.password='aa'] description The se

懒加载session-多数据源springmvc+hibernate 切换问题 在一次请求中多次切换不成功

问题描述 多数据源springmvc+hibernate 切换问题 在一次请求中多次切换不成功 最近写了一个多数据源的代码,结果在使用的过程中出现了在一个请求中(方法中)两个数据库交替使用的情况,最后情况是数据库切换不过来 并且在项目配置中有懒加载和OpenSessionInViewFilter 数据库一个为本地数据库一个为基金数据库 要做的事情就是在本地查询到基金代码接着再循环去基金库查询基金数据 中间报错为在本地数据库中查询不到某个表(其实是在基金库中) controller层 @Reque

springmvc+hibernate服务器上报错,本地没问题

问题描述 springmvc+hibernate服务器上报错,本地没问题 使用springmvc+hibernate在服务器上部署的时候,访问控制器,能够进入控制器打出日志.但是无法显示页面.同时报错. org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException