文章正文

Spring Boot的Component Scan原理

【文档】2020-04-23

简介Spring Boot的Component Scan原理

@ComponentScan

“告诉Spring到哪里搜索”称为组件扫描。

 

Spring Boot项目中的组件扫描

  • 如果你的其他包层次结构位于使用@SpringBootApplication标注主应用程序下方,则隐式组件扫描将自动涵盖。也就是说,不要明确标注@ComponentScan,Spring Boot会自动搜索当前应用主入口及其下方子。

  • 如果其他包中的bean /组件不在当前主包路径下面,,则应手动使用@ComponentScan 添加

  • 如果使用了@ComponentScan ,那么Spring Boot就全部依赖你的定义,如果定义出错,会出现autowired时出错,报a bean of type that could not be found错误。

与组件扫描相关的错误

 No qualifying bean of type found
 或
 在Intellij Idea中显示 incorrectly saying no beans of type found for autowired repository

上述两个问题的根本原因相同 - 组件未被Spring boot发现。

@Component和@ComponentScan有什么区别?

  • @Component表示标记组件。

  • @ComponentScan扫描被标记的组件

打赏支持

感谢您的支持,加油!

打开微信扫码打赏,你说多少就多少

找书费时,联系客服快速查找!

扫码支持

在线客服8:30-22:30,若离线请留言!

获取教程,请联系在线客服!

扫码支持

在线客服8:30-22:30,若离线请留言!

热门阅读

找PDF电子书,太费时间?

  • 微信扫描二维码,让客服快速查找。
  • 在线客服8:30-22:30,若离线请留言!

    PDF电子书