进行Spring Boot和Mybatis进行整合的时候,Spring Boot注解扫描的时候无法扫描到Application类的以外的包下面的注解,如下图:

App就是Application类,下图是ProductMapper 类:

@Mapper
public interface ProductMapper { @Insert("insert into products (pname,type,price)values(#{pname},#{type},#{price}")
public int add(Product product); @Delete("delete from products where id=#{arg1}")
public int deleteById(int id); @Update("update products set pname=#{pname},type=#{type},price=#{price} where id=#{id}")
public int update(Product product); @Select("select * from products where id=#[arg1}")
public Product getById(int id); @Select("select * from productsorder by id desc")
public List<Product> queryByLists();
}

App类运行的时候后台就会报没有找到ProductMapper 这个类bean:

Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type 'com.self.spring.mapper.ProductMapper' available
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:353)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:340)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1090)
at com.self.spring.springboot.App.main(App.java:17)

 

造成上面的错误原因:SpringBoot项目的Bean装配默认规则是根据Application类所在的包位置从上往下扫描! “Application类”是指SpringBoot项目入口类。这个类的位置很关键:

如果Application类所在的包为:io.github.gefangshuai.app,则只会扫描io.github.gefangshuai.app包及其所有子包,如果service或dao所在包不在io.github.gefangshuai.app及其子包下,则不会被扫描!

 

解决方法

第一种:Application类放在父包下面,所有有注解的类放在同一个下面或者其子包下面

第二种:指定要进行扫描注解的包URL,上面的问题的解决方案可以在Application类上面加注解扫描mapper接口包下面的类。

@SpringBootApplication
@MapperScan(basePackages="com.self.spring.springboot.mapper")
public class App {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(App.class, args);
System.out.println(context.getBean(ProductMapper.class));
context.close();
}
}

Spring Boot自动扫描的更多相关文章

  1. Spring Boot 自动扫描组件

    使用@ComponentScan自动扫描组件 案例准备 1.创建一个配置类,在配置类上添加 @ComponentScan 注解.该注解默认会扫描该类所在的包下所有的配置类,相当于之前的 <con ...

  2. 精益求精!Spring Boot 知识点全面回顾,带你重新细读源码!

    约定优于配置 Build Anything with Spring Boot:Spring Boot is the starting point for building all Spring-bas ...

  3. (26)改变自动扫描的包【从零开始学Spring Boot】

    在开发中我们知道Spring Boot默认会扫描启动类同包以及子包下的注解,那么如何进行改变这种扫描包的方式呢,原理很简单就是: @ComponentScan注解进行指定要扫描的包以及要扫描的类. 接 ...

  4. Spring Boot文档阅读

    原因之初 最初习惯百度各种博客教程,然后跟着操作,因为觉得跟着别人走过的路走可以少走很多弯路,省时间.然而,很多博客的内容并不够完整,甚至错误,看多了的博客甚至有千篇一律的感觉.此外,博客毕竟是记载博 ...

  5. 一键式Spring集成工具 Spring Boot

    最近公司使用Spring boot进行开发,稍微了解一下,不过自我感觉把集中式配置applicate.properties搞明白,注解用过Spring MVC的boot绝对没问题的 比如拦截器:@As ...

  6. Spring Boot 性能优化

    spring 框架给企业软件开发者提供了常见问题的通用解决方案,包括那些在未来开发中没有意识到的问题.但是,它构建的 J2EE 项目变得越来越臃肿,逐渐被 Spring Boot 所替代.Spring ...

  7. 基于gralde搭建spring boot项目

    搭建基于gradle的sprint boot项目,swagger-ui辅助 spring boot官网:http://projects.spring.io/spring-boot/get start ...

  8. spring boot配置写法

    转自:http://blog.csdn.net/mickjoust/article/details/51646658 每日金句 你要搞清楚自己人生的剧本:不是你父母的续集,不是你子女的前传,更不是你朋 ...

  9. Spring Boot 集成 Mybatis

    原文:https://github.com/x113773/testall/issues/9 方式一:mybatis-spring-boot-starter---这种方式比较简单,具体步骤如下:1. ...

随机推荐

  1. Codeforces 954 E. Water Taps

    http://codeforces.com/problemset/problem/954/E 式子变成Σ xi*(ti-T)=0 sum0表示>=T的ai*ti之和 sum1表示<T的ai ...

  2. mysql Mac终端操作

    1.启动mysql :brew services start mysql 2.登陆mysql :  mysql -u root -p mysql 命令.      -u 后面接用户名 root超级管理 ...

  3. 使用ResourceBundle读取配置文件

    在Java语言中,使用一种以.properties为扩展名的文本文件作为资源文件,该类型的文件的内容格式为类似: 12 #注释语句 some_key=some_value 形式.以#开头的行作为注释行 ...

  4. 【洛谷P2704【NOI2001】】炮兵阵地

    题目描述 司令部的将军们打算在N*M的网格地图上部署他们的炮兵部队.一个N*M的地图由N行M列组成,地图的每一格可能是山地(用“H” 表示),也可能是平原(用“P”表示),如下图.在每一格平原地形上最 ...

  5. <video>标签:视频播放器动态设置src

    HTML代码 <div  id="my_div">    <video id="my_video"  width="600" ...

  6. Java 的NIO 3个主要概念 Channel、Buffer、Selector

    Java 的NIO 3个主要概念 Channel.Buffer.Selector,为何提高了性能

  7. Freemarker导出带格式的word的使用

    1.新建一个doc文档

  8. linux笔记_day05

    1.bash以及特性 shell:外壳 GUI:KDE,Gnome,Xfce CLI:sh,csh,ksh,bash(born again shell) 进程:在每个进程看来,当前主机上只存在内核和当 ...

  9. jQuery中对未来的元素绑定事件用bind、live or on

    对未来的元素绑定事件不能用bind, 1.可以用live代替,但是要注意jquery的版本,根据官方文档,从1.7开始就不推荐live和delegate了,1.9里就去掉live了. 2.推荐用on代 ...

  10. TypeError: 'range' object does not support item assignment

    TypeError: 'range' object does not support item assignment I was looking at some python 2.x code and ...