问题描述
弄了好长时间,老是报异常异常如下Errorcreatingbeanwithname'userController':Injectionofautowireddependenciesfailed;nestedexceptionisorg.springframework.beans.factory.BeanCreationException:Couldnotautowirefield:com.yihaomen.mybatis.inter.IUserOperationcom.yihaomen.controller.UserController.userMapper;nestedexceptionisorg.springframework.beans.factory.NoSuchBeanDefinitionException:Nomatchingbeanoftype[com.yihaomen.mybatis.inter.IUserOperation]foundfordependency:expectedatleast1beanwhichqualifiesasautowirecandidateforthisdependency.Dependencyannotations:{@org.springframework.beans.factory.annotation.Autowired(required=true)}applicationContext:<?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-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd"default-autowire="byName"><context:property-placeholderlocation="classpath:/config/database.properties"/><beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close"p:driverClassName="com.mysql.jdbc.Driver"p:url="jdbc:mysql://127.0.0.1:3306/test"p:username="root"p:password="root"p:maxActive="10"p:maxIdle="10"></bean><beanid="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><propertyname="dataSource"ref="dataSource"/></bean><beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean"><!--dataSource属性指定要用到的连接池--><propertyname="dataSource"ref="dataSource"/><!--configLocation属性指定mybatis的核心配置文件--><propertyname="configLocation"value="classpath*:config/Configuration.xml"/><!--所有配置的mapper文件--><propertyname="mapperLocations"value="classpath*:com/yihaomen/mybatis/model/*.xml"/></bean><beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer"><propertyname="basePackage"value="com.yihaomen.mybatis.inter"/></bean></beans>
Configuration.xml<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEconfigurationPUBLIC"-//mybatis.org//DTDConfig3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><typeAliases><typeAliasalias="User"type="com.yihaomen.mybatis.model.User"/><typeAliasalias="Article"type="com.yihaomen.mybatis.model.Article"/></typeAliases></configuration>
UserController.java:packagecom.yihaomen.controller;importjava.util.List;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.servlet.ModelAndView;importcom.yihaomen.mybatis.inter.IUserOperation;importcom.yihaomen.mybatis.model.Article;@Controller@RequestMapping("/article")publicclassUserController{@AutowiredIUserOperationuserMapper;@RequestMapping("/list")publicModelAndViewlistall(HttpServletRequestrequest,HttpServletResponseresponse){List<Article>articles=userMapper.getUserArticles(1);ModelAndViewmav=newModelAndView("list");mav.addObject("articles",articles);returnmav;}}
IUserOperation.java:packagecom.yihaomen.mybatis.inter;importjava.util.List;importcom.yihaomen.mybatis.model.Article;importcom.yihaomen.mybatis.model.User;publicinterfaceIUserOperation{publicUserselectUserByID(intid);publicList<User>selectUsers(StringuserName);publicvoidaddUser(Useruser);publicvoidupdateUser(Useruser);publicvoiddeleteUser(intid);publicList<Article>getUserArticles(intid);publicList<Article>getUserArticles_2(intid);}
以上这些方法都是在User.xml中配置实现的本人新手,还望有大神指教
解决方案
解决方案二:
com.yihaomen.mybatis.inter.IUserOperation是不是不在classpath路径里?
解决方案三:
引用1楼shixitong的回复:
com.yihaomen.mybatis.inter.IUserOperation是不是不在classpath路径里?
我是放在了一个叫user_src的目录下,意思是这个必须放在src目录下么?
解决方案四:
引用1楼shixitong的回复:
com.yihaomen.mybatis.inter.IUserOperation是不是不在classpath路径里?
我放在了src目录下面,好像还是不行