当前位置: 首页 > article >正文

【SpringBoot】一文讲懂什么是scanBasePackages

文章目录

  • 一、作用
    • 流程说明
  • 二、使用场景
  • 三、配置方式
  • 四、默认行为
  • 五、注意事项
  • 六、示例
    • 1. 单模块项目
    • 2. 多模块项目
  • 七、与@ComponentScan的关系
  • 八、总结

scanBasePackages是SpringBoot中用于指定主件扫描(Component Scanning)的包路径的配置属性。它通常用于@SpringBootApplication注解中,用于控制SpringBoot应用程序启动时扫描哪些包中的组件(如@Component@Service@Respository@Controller等)。

一、作用

  • 指定扫描范围:默认情况下,SpringBoot 会扫描主应用程序类所在包及其子包中的所有组件。通过 scanBasePackages,可以自定义扫描的包路径。
  • 优化启动性能:减少不必要的包扫描,加快应用程序启动速度。
  • 模块化支持:在多模块项目中,指定需要扫描的模块包。
    在这里插入图片描述

流程说明

1.Spring Boot 启动

  • 应用程序启动时,Spring Boot 开始初始化 Spring 上下文。

2.检查是否指定 scanBasePackages

  • 如果指定了 scanBasePackages,Spring Boot 会扫描指定的包及其子包。
  • 如果未指定 scanBasePackages,Spring Boot 会扫描主类所在包及其子包。

3.扫描并注册组件

  • Spring Boot 会扫描包中的组件(如 @Component@Service@Repository@Controller 等)。
  • 将所有符合条件的组件注册到 Spring 上下文中。

4.完成 Spring 上下文初始化

  • 扫描完成后,Spring 上下文初始化完成,应用程序可以正常运行。

二、使用场景

  • 1.多模块项目
    主模块需要扫描其他模块的组件。
  • 2.自定义包结构
    项目包结构与默认扫描路径不一致。
  • 3.性能优化
    避免扫描不必要的包。

三、配置方式

1. 在 @SpringBootApplication 中配置

@SpringBootApplication(scanBasePackages = {"com.example.core", "com.example.web"})
public class MyApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}

2.使用 @ComponentScan 注解

@SpringBootApplication
@ComponentScan(basePackages = {"com.example.core", "com.example.web"})
public class MyApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}

四、默认行为

  • 如果不指定 scanBasePackages,Spring Boot 会扫描主应用程序类所在包及其子包。
  • 例如,主类在 com.example 包下,则默认扫描 com.example 及其子包。

五、注意事项

  1. 扫描范围
    如果指定了 scanBasePackages,Spring Boot 只会扫描指定的包,而不会扫描默认包。
  2. 性能影响
    扫描范围过大可能会影响启动性能,建议根据实际需求配置。
  3. 多模块项目
    确保所有需要扫描的模块包都已包含在 scanBasePackages 中。

六、示例

1. 单模块项目

@SpringBootApplication(scanBasePackages = "com.example")
public class MyApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}

2. 多模块项目

@SpringBootApplication(scanBasePackages = {"com.example.core", "com.example.web"})
public class MyApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
}

七、与@ComponentScan的关系

  • scanBasePackages@SpringBootApplication 的一个属性,用于简化配置。
  • @ComponentScan 是 Spring 框架的原生注解,功能更强大,支持更复杂的扫描规则。
  • 如果同时使用 scanBasePackages@ComponentScan@ComponentScan 的配置会覆盖 scanBasePackages

八、总结

特性说明
默认行为扫描主类所在包及其子包
自定义扫描范围通过 scanBasePackages@ComponentScan 指定
性能优化减少不必要的包扫描,提升启动速度
多模块支持指定需要扫描的模块包

通过合理配置 scanBasePackages,可以更好地控制 Spring Boot 应用程序的组件扫描行为,满足复杂项目的需求。


http://www.kler.cn/a/573013.html

相关文章:

  • [MySQL初阶]MySQL(3)表的约束
  • 华为最新OD机试真题-计算堆栈中的剩余数字-Python-OD统一考试(E卷)
  • C语言学习笔记-进阶(1)深入理解指针3
  • Ollama+AnythingLLM安装
  • 期权有哪些用处?期权和期货比优势在哪?
  • CentOS 7 安装 Redis6.2.6
  • R语言绘图:韦恩图
  • 06. View工作原理
  • 《HarmonyOS赋能的智能影像诊断系统安全架构与临床实践》
  • 杨辉三角解法
  • kotlin的val声明的变量是常量吗
  • vscode 都有哪些大模型编程插件
  • Raven: 2靶场渗透测试
  • 如何在Android中实现自定义视图
  • 软考-数据库开发工程师-3.1-数据结构-线性结构
  • 统计Excel列中某值出现的次数
  • 【消息队列】数据库的数据管理
  • pt-archiver删除数据库的数据表/各种报错类型
  • 字典的setdefault()
  • 六十天前端强化训练之第九天之数组操作方法