问题描述
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。