码迷,mamicode.com
首页 > 编程语言 > 详细

springboot+mybatis遇到BUG

时间:2019-04-13 22:06:45      阅读:292      评论:0      收藏:0      [点我收藏+]

标签:扫描   自动   mybatis   frame   shu   autowired   RoCE   pre   1.3   

今天用springboot+mybatis写一个小demo遇到如下错误

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-04-08 14:11:11.359 ERROR 7300 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Field dao in cn.niit.controller.DemoController required a bean of type 'cn.niit.TestMapper.TestMapper' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'cn.niit.TestMapper.TestMapper' in your configuration.


Process finished with exit code 1

它说没有找到这个类,说明这个类没有注入到spring容器,以为是注解没有被扫描,然后各种网上搜以及自己各种重启尝试,了解到@SpringBootApplication没有扫描到

 正常情况下加上@Component注解的类会自动被Spring扫描到生成Bean注册到spring容器中,既然他说没找到,也就是该注解被没有被spring识别,问题的核心关键就在application类的注解SpringBootApplication上 ,@SpringBootApplication这个注解扫描该类同级包下的类以及子包的类

我换包的位置后各种尝试还是不行,接着再了解到

说是在启动类上加上@ComponentScan,还是没能解决,说是@ComponentScan和@SpringBootApplication扫描包覆盖:

@SpringBootApplication=@Configuration+@EnableAutoConfiguration+@ComponentScan,其中扫描包的范围为启动类所在包和子包,不包括第三方的jar包。如果我们需要扫描通过maven依赖添加的jar,我们就要单独使用@ComponentScan注解扫描第三方包。
但是,如果@SpringBootApplication和@ComponentScan注解共存,那么@SpringBootApplication注解的扫描的作用将会失效,也就是说不能够扫描启动类所在包以及子包了。因此,我们必须在@ComponentScan注解配置本工程需要扫描的包范围。
技术图片

结果还是没能解决,最后拿另一个项目对比着检查,才发现是pom.xml中的依赖,我导的是mybatis,而正确的依赖应该是导入mybatis-spring-boot-starter这个依赖(难受,@Mapper这个注解引入的都是import org.apache.ibatis.annotations.Mapper;)

技术图片

个人网站

springboot+mybatis遇到BUG

标签:扫描   自动   mybatis   frame   shu   autowired   RoCE   pre   1.3   

原文地址:https://www.cnblogs.com/panbingwen/p/10703000.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!