Spring Boot自动扫描
进行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自动扫描的更多相关文章
- Spring Boot 自动扫描组件
使用@ComponentScan自动扫描组件 案例准备 1.创建一个配置类,在配置类上添加 @ComponentScan 注解.该注解默认会扫描该类所在的包下所有的配置类,相当于之前的 <con ...
- 精益求精!Spring Boot 知识点全面回顾,带你重新细读源码!
约定优于配置 Build Anything with Spring Boot:Spring Boot is the starting point for building all Spring-bas ...
- (26)改变自动扫描的包【从零开始学Spring Boot】
在开发中我们知道Spring Boot默认会扫描启动类同包以及子包下的注解,那么如何进行改变这种扫描包的方式呢,原理很简单就是: @ComponentScan注解进行指定要扫描的包以及要扫描的类. 接 ...
- Spring Boot文档阅读
原因之初 最初习惯百度各种博客教程,然后跟着操作,因为觉得跟着别人走过的路走可以少走很多弯路,省时间.然而,很多博客的内容并不够完整,甚至错误,看多了的博客甚至有千篇一律的感觉.此外,博客毕竟是记载博 ...
- 一键式Spring集成工具 Spring Boot
最近公司使用Spring boot进行开发,稍微了解一下,不过自我感觉把集中式配置applicate.properties搞明白,注解用过Spring MVC的boot绝对没问题的 比如拦截器:@As ...
- Spring Boot 性能优化
spring 框架给企业软件开发者提供了常见问题的通用解决方案,包括那些在未来开发中没有意识到的问题.但是,它构建的 J2EE 项目变得越来越臃肿,逐渐被 Spring Boot 所替代.Spring ...
- 基于gralde搭建spring boot项目
搭建基于gradle的sprint boot项目,swagger-ui辅助 spring boot官网:http://projects.spring.io/spring-boot/get start ...
- spring boot配置写法
转自:http://blog.csdn.net/mickjoust/article/details/51646658 每日金句 你要搞清楚自己人生的剧本:不是你父母的续集,不是你子女的前传,更不是你朋 ...
- Spring Boot 集成 Mybatis
原文:https://github.com/x113773/testall/issues/9 方式一:mybatis-spring-boot-starter---这种方式比较简单,具体步骤如下:1. ...
随机推荐
- Neural Networks and Deep Learning 课程笔记(第四周)深层神经网络(Deep Neural Networks)
1. 深层神经网络(Deep L-layer neural network ) 2. 前向传播和反向传播(Forward and backward propagation) 3. 总结 4. 深层网络 ...
- webx roadmap
SpringExt 自定义Spring Schema的例子 基于Spring可扩展Schema提供自定义配置支持 使用SpringExt扩展Webx的示例 扩展点和捐献 一个namespace下可以声 ...
- Javaweb学习笔记——(二)——————CSS概述,进入JavaScript
day02day01内容回顾 1.html操作思想 **使用标签把要操作的数据包起来,通过修改标签的属性值来是实现标签内数据样式的变化 ***<font size="5"&g ...
- static, const
static 静态的,类的静态成员函数,静态成员变量是和类相关的,但不和具体对象相关.即使没有具体对象,也能调用类的静态成员函数和成员变量.一般类的静态函数就是一个全局函数,只是作用域在包含它的文件中 ...
- 第15月第6天 ios UIScrollView不能响应TouchesBegin
1. 1:@property MyScrollView *scrollView; 2:给MyScrollView,增加类别:MyScrollView+Touch 3:在类别里实现下面三个方法: @im ...
- linux C sscanf()函数
linux sscanf() 类似正则表达式,又不完全是正则表达式. 分割 ”/“ 或 "@" 或空格 要用 [^/] 例如: sscanf("iios/12DDWDFF ...
- web前端最全各类资源
链接:http://www.sohu.com/a/157593700_132276
- 基于Python的机器学习实战:Apriori
目录: 1.关联分析 2. Apriori 原理 3. 使用 Apriori 算法来发现频繁集 4.从频繁集中挖掘关联规则 5. 总结 1.关联分析 返回目录 关联分析是一种在大规模数据集中寻找有趣 ...
- 创建虚拟机时,提示No valid host was found解决办法
1.http://blog.csdn.net/yxwmzouzou/article/details/43892261 2.http://www.cnblogs.com/kevingrace/p/601 ...
- db_recovery_file_dest_size
select name,space_limit,space_used,number_of_files from v$recovery_file_dest; alter system set db_re ...