博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring Boot:Consider defining a bean of type '*.*.*' in your configuration解决方案
阅读量:6428 次
发布时间:2019-06-23

本文共 4259 字,大约阅读时间需要 14 分钟。

  果然不看教程直接使用在遇到问题会懵逼,连解决问题都得搜半天还不一定能帮你解决了。。。

***************************

APPLICATION FAILED TO START
***************************
Description:
Field mapper in com.demo.service.impl.UserServiceImpl required a bean of type 'com.demo.mapper.UserMapper' that could not be found.
Action:
Consider defining a bean of type 'com.demo.mapper.UserMapper' in your configuration.

SpringBoot启动失败,告诉我Bean配置失败,楼主看了看  该用的注解都用上了  这是咋的回事嘞?

mapper(Dao层)

1 package com.demo.mapper; 2  3 import org.springframework.context.annotation.ComponentScan; 4 import org.springframework.stereotype.Repository; 5  6 import com.demo.domain.User; 7  8 //@Component 9 @Repository10 public interface UserMapper {11 12     public User gYeMian(User u);13 14     public int sYeMian(User u);15 16 }

service

package com.demo.service.impl;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import com.demo.domain.User;import com.demo.mapper.UserMapper;import com.demo.service.UserService;@Service(value = "userService")public class UserServiceImpl implements UserService{        @Autowired    private UserMapper mapper;    @Override    public User gYeMian(User u) {        User user = mapper.gYeMian(u);        return user;    }    @Override    public int sYeMian(User u) {        int i = mapper.sYeMian(u);        return i;    }}

controller

1 package com.demo.controller; 2  3 import javax.servlet.http.HttpServletRequest; 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.bind.annotation.ResponseBody; 9 10 import com.demo.domain.User;11 import com.demo.service.UserService;12 13 @Controller14 @RequestMapping(value = "/uc")15 public class UserController {16     17     @Autowired18     private UserService userService;19     20     @ResponseBody21     @RequestMapping("/stemp.htm")22     private String sYeMian(String muBan, HttpServletRequest request){23 24         User u = new User();25         u.setMuBan(muBan);26         System.out.println("muBan=" + muBan);27         int i = userService.sYeMian(u);28         29         if (i>0){30             return "存储成功";31         }32             return "存储失败";33     }34 35 }

后来在网上看到网友说要用@Mapper注解,这才把问题解决了   至于具体原因,楼主还需要好好看看文档再来解释。

解决方案一:

mapper(Dao层)

1 package com.demo.mapper; 2  3 import org.apache.ibatis.annotations.Mapper; 4  5 import com.demo.domain.User; 6  7 @Mapper 8 public interface UserMapper { 9 10     public User gYeMian(User u);11 12     public int sYeMian(User u);13 14 }

 解决方案二:

Application(启动类)

1 package com.demo; 2  3 import org.mybatis.spring.annotation.MapperScan; 4 import org.springframework.boot.SpringApplication; 5 import org.springframework.boot.autoconfigure.SpringBootApplication; 6  7 @SpringBootApplication 8 @MapperScan(value = "com.demo.mapper") 9 public class App 10 {11     public static void main(String[] args) throws Exception {12         SpringApplication.run(App.class, args);13     }14 }

原因:在mybatis-spring-boot-autoconfigure的jar包中有一个类 MybatisAutoConfiguration,在这个类中的registerBeanDefinitions方法告诉了我们

1 @Override 2     public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { 3  4       logger.debug("Searching for mappers annotated with @Mapper"); 5  6       ClassPathMapperScanner scanner = new ClassPathMapperScanner(registry); 7  8       try { 9         if (this.resourceLoader != null) {10           scanner.setResourceLoader(this.resourceLoader);11         }12 13         List
packages = AutoConfigurationPackages.get(this.beanFactory);14 if (logger.isDebugEnabled()) {15 for (String pkg : packages) {16 logger.debug("Using auto-configuration base package '{}'", pkg);17 }18 }19 20 scanner.setAnnotationClass(Mapper.class);21 scanner.registerFilters();22 scanner.doScan(StringUtils.toStringArray(packages));23 } catch (IllegalStateException ex) {24 logger.debug("Could not determine auto-configuration package, automatic mapper scanning disabled.", ex);25 }26 }

 

转载于:https://www.cnblogs.com/JealousGirl/p/bean.html

你可能感兴趣的文章
Lucene.Net 2.3.1开发介绍 —— 四、搜索(一)
查看>>
MyBatis Review——开发Dao的方法
查看>>
技术研发国产化进程加快 看传感器企业如何展示十八般武艺
查看>>
技术助力第三次革命
查看>>
《HTML与CSS入门经典(第8版)》——2.6 总结
查看>>
新手指南:在 Ubuntu 和 Fedora 上安装软件包
查看>>
在 CentOS7.0 上搭建 Chroot 的 Bind DNS 服务器
查看>>
大型网站的 HTTPS 实践(二):HTTPS 对性能的影响
查看>>
《Swift 权威指南》——第6章,第6.10节嵌套函数
查看>>
《自己动手做交互系统》——1.3 本章小结
查看>>
Mobile devices bundled with malware?
查看>>
《JavaScript面向对象精要》——1.5 访问属性
查看>>
《Python数据可视化编程实战》—— 第 1 章 准备工作环境
查看>>
《设计模式解析(第2版•修订版)》—第2章 2.2节什么是UML
查看>>
【直播】APP全量混淆和瘦身技术揭秘
查看>>
10个大坑,当你产品上架AppStore会遇到
查看>>
【shell 脚本】两种登录方式
查看>>
升级linux自带的Python
查看>>
百度地图2.0瓦片地址获取(窗口内瓦片)
查看>>
我的友情链接
查看>>