# spring-boot-scan-packages-example **Repository Path**: fox-demo/spring-boot-scan-packages-example ## Basic Information - **Project Name**: spring-boot-scan-packages-example - **Description**: SpringBoot启动类自动包扫描 四种方式 - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2022-03-16 - **Last Updated**: 2023-06-27 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # spring-boot-scan-packages-example SpringBoot启动类自动包扫描(扫描第三方包) 四种方式 >com.fox: 为第三方包路径 ## 方式一 @SpringBootApplication 中 scanBasePackages 引入包 请看 example 案例 ```java @SpringBootApplication(scanBasePackages={"com.fox"}) ``` ## 方式二 配置 BeanConfigScanConfig 写好注解 请看 example2 案例 请看 BeanConfigScanConfig 文件 ```java @ComponentScans(value = {@ComponentScan(value = "com.fox")}) @EntityScan(basePackages = {"com.fox"}) @Configuration public class BeanConfigScanConfig implements EnvironmentAware { @Override public void setEnvironment(Environment environment) { System.out.println("##################################初始化 BeanConfigScan ################################################"); } } ``` 注解包 扫描有顺序 > com.fox : 为第三包路径 > ## 方式三 在 SpringBoot Application 启动文件中 配置注解 @ComponentScan 请看 example3 案例 编辑 Example3Application 文件,注解如下 ```java @SpringBootApplication @ComponentScan(value = {"com.example", "com.fox"}) ``` 注解包 扫描有顺序 > com.example : 为 当前项目包路径 > > com.fox : 为第三包路径 > ## 方式四 在第三方包内(有权限修改) 配置 BeanConfigScanConfig 写好注解,最后配置 spring.factories 请看 example5 案例 编辑配置文件 BeanConfigScanConfig ```java @ComponentScans(value = {@ComponentScan(value = "com.fox")}) @EntityScan(basePackages = {"com.fox"}) @Configuration public class BeanConfigScanConfig implements EnvironmentAware { @Override public void setEnvironment(Environment environment) { System.out.println("##################################初始化 BeanConfigScan ################################################"); } } ``` 在resources 文件夹下创建 META-INF 文件夹,在META-INF 文件夹内创建 spring.factories 文件 `spring.factories` 文件内容如下 ```java org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.example.configuration.BeanConfigScanConfig ```