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. ...
随机推荐
- Gym - 100851F - Froggy Ford(dijkstra)
题目链接 参考 http://blog.csdn.net/KIJamesQi/article/details/52214990 题意 蛤蛤要从这岸去到对岸,河中有n块石头,现可以在河中添加一块石头 ...
- Gitlab部署
前言 因部门业务整顿,需将原有的多部门共用的Gitlab环境遗弃,新建部门独立的Gitlab环境! 安装 CE版本安装操作:https://about.gitlab.com/install/#cent ...
- 从池子里的beta看秋香, 个性迥异
从池子里的beta看秋香, 个性迥异 前文里那十只个股为例, 统计了它们的beta值. 回顾如下: Num name code Beta 0 深圳燃气 601139 0.710 公用事业 1 分众传媒 ...
- utf8_bin跟utf8_general_ci的区别
ci是 case insensitive, 即 "大小写不敏感", a 和 A 会在字符判断中会被当做一样的; bin 是二进制, a 和 A 会别区别对待. 例如你运行: SEL ...
- js 报delete object in strict mode
JAVA->Compiler->Building->No strictly compatible JRE for execution environment available Ig ...
- 20155332 2016-2017-2 《Java程序设计》第5周学习总结
学号 2016-2017-2 <Java程序设计>第X周学习总结 教材学习内容总结 1.Java中的所有不正常类都继承于Throwable类.Throwable主要包括两个大类,一个是Er ...
- OBS 录制视频 自己留存
1. 下载安装 软件下载地址:https://github.com/jp9000/obs-studio/releases/download/19.0.2/OBS-Studio-19.0.2-Full- ...
- cdn.dns,cms
CDN CDN的全称是Content Delivery Network,即内容分发网络.其基本思路是尽可能避开互联网上有可能影响数据传输速度和稳定性的瓶颈和环节,使内容传输的更快.更稳定.通过在网络各 ...
- Linux input子系统简介
1.前言 本文主要对Linux下的input子系统进行介绍 2. 软件架构 图 input子系统结构图 input子系统主要包括三个部分:设备驱动层.核心层和事件层.我们可以分别理解为:具体的输入设备 ...
- Github简介
先附上下载地址 http://windows.github.com/ git-scm.com是版本控制软件Git的官方网站. Git和GitHub的区别 Git是一个分布式的版本控制系统,与SVN类似 ...