zoukankan      html  css  js  c++  java
  • springboot+mybatis遇到BUG:自动注入失败

    今天用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注解配置本工程需要扫描的包范围。
    image.png

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

    个人网站

  • 相关阅读:
    数据结构、算法、及线性表总结
    第二次博客作业: 函数+进制转换器v1.0beta
    c语言文件
    Oracle中Left Outer Join和外关联(+)的区别
    Oracle中trunc函数、round 函数、ceil函数和floor 函数的使用
    Oracle 表之间的连接 JOIN
    Oracle TRUNCATE语法
    使用Content editor webpart 为NewForm增加默认值
    Metadata serviceTaxonomyHiddenList 权限
    SQL server总是不能远程连接
  • 原文地址:https://www.cnblogs.com/panbingwen/p/10703000.html
Copyright © 2011-2022 走看看