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”,

  错误代码:

 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
Context initialization failed
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)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1120)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:607)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:647)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:598)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:661)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:517)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:458)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:138)
at javax.servlet.GenericServlet.init(GenericServlet.java:160)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1280)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1193)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1088)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5176)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5460)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
at java.util.concurrent.FutureTask.run(FutureTask.java:166)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:722)
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]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:514)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)
... 30 more
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]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:825)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:730)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:486)
... 32 more

  相关代码:

這是接口实现类

 package com.kjczwl.ssm.service.impl;

 import java.util.List;

 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import com.kjczwl.ssm.mapper.ItemsMapperCustom;
import com.kjczwl.ssm.po.ItemsCustom;
import com.kjczwl.ssm.po.ItemsQueryVo;
import com.kjczwl.ssm.service.ItemsService; /**
* 商品管理
* @author 唐烈
*
*/
@Service
public class ItemsServiceImpl implements ItemsService{
@Autowired
ItemsMapperCustom itemsMapperCustom;
@Override
public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception {
//通过ItemsMapperCustom来查询数据库 通过注解自动扫描 所以這里什么都不用写
return itemsMapperCustom.findItemsList(itemsQueryVo);
} }

controller类

 package com.kjczwl.ssm.controller;

 import java.util.List;

 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.portlet.ModelAndView;
import com.kjczwl.ssm.po.ItemsCustom;
import com.kjczwl.ssm.service.ItemsService; /**
*<p>package: com.kjczwl.ssm.controller</p>
*<p>Description:商品的controller(Handler) </p>
*<p>Company: Springmvc_Mybits_store</p>
*@author: 唐烈
* @date 2017下午5:30:58
*/
@Controller// 注解模式开发Controller
public class ItemsController {
// 注入 从service 中取得数据
@Autowired
ItemsService itemsservice; //错误问题出现這里

//注解扫描, 后面映射地址 可通过queryItems.action 访问
@RequestMapping("/queryItems")
public ModelAndView queryItems()throws Exception{
//得到数据
List<ItemsCustom> itemsList = itemsssService.findItemsList(null);
// 构造ModelAndView
ModelAndView modelAndView = new ModelAndView();
// 添加到内存区 外面直接“${} 条件表达式获取”
modelAndView.addObject("itemsList", itemsList);
//转发的路径
modelAndView.setViewName("pages/itemsList"); return modelAndView;
}
}

applicationContext-service.xml 配置

     <beans
xmlns="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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframectivework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
<!-- 商品管理的service -->
<bean id="itemsService" class="com.kjczwl.ssm.service.impl.ItemsServiceImpl"/>
</beans>
上一篇:SQL truncate 、delete与drop区别


下一篇:IOS开发之UISearchBar自定义外观