前言:
1、大家都知道,Springboot主启动加载会默认扫描同级包目录下所有的组件类、配置类,然后进行解析注入到Spring容器中。@SpringBootApplication 是个联合注解,里面包含了 @ComponentScan 组件扫描注解,所以我们不需要单独在主启动类上标注 @ComponentScan
2、如果我们需要扫描主启动类同级以外的包目录怎么?下面看下 @ComponentScans和@ComponentScan 的使用
3、验证用的项目结构为:
一、使用 @ComponentScans 指明需要扫描的包目录
1、示例代码:
@ComponentScans(value = {@ComponentScan(basePackages = {"test.componentscans.configure", "test.componentscans.dto"})})
说明:
【1】@ComponentScans 的属性值 value 为 @ComponentScan 类型的数组
【2】@ComponentScan 的 value 或 basePackages 为具体要扫描的包目录
【3】basePackages 的值支持包全路径、也支持通配符,例如:com.xxx.*
【4】