spring整合mybatis错误:Could not autowire field: com.kjczwl.ssm.service.ItemsService com.kjczwl.ssm.controller.ItemsController.itemsservice;

  运行环境:jdk1.7.0_17+tomcat 7 + spring:3.2.0 +mybatis:3.2.7+ eclipse

  错误:Could not autowire field: com.kjczwl.ssm.service.ItemsService com.kjczwl.ssm.controller.ItemsController.itemsservice; 

  错误原因:spring的@Autowired 无法注入service的bean(spring+mybatis) 

  解决办法: 在配置时候,写的mapper扫描注解,并且在接口实现出注解@Service都不能解决问题,在学习群中问了写大神,后面仔细检查了下,发现注解service id和 我的controller 里面注解引用的的不一样,我的service id “itemsService”,controller         “itemsservice”,

  错误代码:

 1 Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@5c620819: defining beans [itemsController,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,itemsServiceImpl,mvcContentNegotiationManager,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0,org.springframework.format.support.FormattingConversionServiceFactoryBean#0,org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter#0,org.springframework.web.servlet.handler.MappedInterceptor#0,org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver#0,org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver#0,org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver#0,org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping,org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter,org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler#0,org.springframework.web.servlet.handler.SimpleUrlHandlerMapping#0,org.springframework.web.servlet.view.InternalResourceViewResolver#0,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor]; parent: org.springframework.beans.factory.support.DefaultListableBeanFactory@1afd5978
 2 Context initialization failed
 3 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'itemsController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.kjczwl.ssm.service.ItemsService com.kjczwl.ssm.controller.ItemsController.itemsssService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.kjczwl.ssm.service.ItemsService] is defined: expected single matching bean but found 2: [itemsServiceImpl, itemsService]
 4     at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288)
 5     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1120)
 6     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522)
 7     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
 8     at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
 9     at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
10     at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
11     at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
12     at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:607)
13     at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
14     at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
15     at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:647)
16     at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:598)
17     at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:661)
18     at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:517)
19     at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:458)
20     at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:138)
21     at javax.servlet.GenericServlet.init(GenericServlet.java:160)
22     at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1280)
23     at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1193)
24     at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1088)
25     at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5176)
26     at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5460)
27     at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
28     at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
29     at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
30     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
31     at java.util.concurrent.FutureTask.run(FutureTask.java:166)
32     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
33     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
34     at java.lang.Thread.run(Thread.java:722)
35 Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.kjczwl.ssm.service.ItemsService com.kjczwl.ssm.controller.ItemsController.itemsssService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.kjczwl.ssm.service.ItemsService] is defined: expected single matching bean but found 2: [itemsServiceImpl, itemsService]
36     at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:514)
37     at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
38     at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)
39     ... 30 more
40 Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.kjczwl.ssm.service.ItemsService] is defined: expected single matching bean but found 2: [itemsServiceImpl, itemsService]
41     at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:825)
42     at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:730)
43     at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:486)
44     ... 32 more

  相关代码:

這是接口实现类

 1 package com.kjczwl.ssm.service.impl;
 2 
 3 import java.util.List;
 4 
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.stereotype.Service;
 7 
 8 import com.kjczwl.ssm.mapper.ItemsMapperCustom;
 9 import com.kjczwl.ssm.po.ItemsCustom;
10 import com.kjczwl.ssm.po.ItemsQueryVo;
11 import com.kjczwl.ssm.service.ItemsService;
12 
13 /**
14      *   商品管理
15      * @author 唐烈
16      *
17      */
18 @Service
19 public class ItemsServiceImpl implements ItemsService{
20     @Autowired
21         ItemsMapperCustom itemsMapperCustom;
22     @Override
23     public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception {
24         //通过ItemsMapperCustom来查询数据库 通过注解自动扫描  所以這里什么都不用写
25         return itemsMapperCustom.findItemsList(itemsQueryVo);
26     }
27     
28 }

controller类

 1 package com.kjczwl.ssm.controller;
 2 
 3 import java.util.List;
 4 
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.stereotype.Controller;
 7 import org.springframework.web.bind.annotation.RequestMapping;
 8 import org.springframework.web.portlet.ModelAndView;
 9 import com.kjczwl.ssm.po.ItemsCustom;
10 import com.kjczwl.ssm.service.ItemsService;
11 
12 /**
13  *<p>package:  com.kjczwl.ssm.controller</p>
14  *<p>Description:商品的controller(Handler) </p> 
15  *<p>Company: Springmvc_Mybits_store</p>
16  *@author:         唐烈
17  * @date            2017下午5:30:58
18  */
19 @Controller// 注解模式开发Controller
20 public class ItemsController {
21     // 注入 从service 中取得数据
22     @Autowired
23     ItemsService itemsservice; //错误问题出现這里
24     
25     //注解扫描, 后面映射地址  可通过queryItems.action 访问
26     @RequestMapping("/queryItems")
27     public ModelAndView queryItems()throws Exception{
28         //得到数据
29         List<ItemsCustom> itemsList = itemsssService.findItemsList(null);
30         // 构造ModelAndView 
31         ModelAndView modelAndView = new ModelAndView();
32         // 添加到内存区 外面直接“${} 条件表达式获取”
33         modelAndView.addObject("itemsList", itemsList);
34         //转发的路径
35         modelAndView.setViewName("pages/itemsList");
36         
37         return modelAndView;
38     }
39 }

applicationContext-service.xml 配置

 1     <beans
 2         xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:p="http://www.springframework.org/schema/p"
 5        xmlns:context="http://www.springframework.org/schema/context"
 6        xmlns:mvc="http://www.springframework.org/schema/mvc"
 7         xmlns:tx="http://www.springframework.org/schema/tx"
 8         xmlns:aop="http://www.springframework.org/schema/aop"
 9           xsi:schemaLocation="http://www.springframework.org/schema/beans 
10                                       http://www.springframework.org/schema/beans/spring-beans.xsd
11                                       http://www.springframework.org/schema/context
12                                     http://www.springframework.org/schema/context/spring-context-3.2.xsd
13                                     http://www.springframework.org/schema/mvc
14                                     http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
15                                     http://www.springframework.org/schema/tx
16                                     http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
17                                     http://www.springframectivework.org/schema/aop
18                                     http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
19     <!-- 商品管理的service -->
20     <bean id="itemsService" class="com.kjczwl.ssm.service.impl.ItemsServiceImpl"/>
21 </beans>

 

posted @ 2017-04-06 15:26  人生在与不断学习。  阅读(18935)  评论(2编辑  收藏  举报