SpringBoot启动类自动包扫描 三种方式

spring-boot-scan-packages-example

SpringBoot启动类自动包扫描 三种方式
fox.风

com.fox: 为第三方包路径

方式一 @SpringBootApplication 中 scanBasePackages 引入包

请看 example 案例

@SpringBootApplication(scanBasePackages={"com.fox"})

方式二 配置 BeanConfigScanConfig 写好注解

请看 example2 案例

请看 BeanConfigScanConfig 文件

@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 文件,注解如下

@SpringBootApplication
@ComponentScan(value = {"com.example", "com.fox"})

注解包 扫描有顺序

com.example : 为 当前项目包路径

com.fox : 为第三包路径

方式四 在第三方包内(有权限修改) 配置 BeanConfigScanConfig 写好注解,最后配置 spring.factories

请看 example5 案例

编辑配置文件 BeanConfigScanConfig

@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 文件内容如下

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
        com.example.configuration.BeanConfigScanConfig

代码: https://gitee.com/fox-demo/spring-boot-scan-packages-example

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风.foxwho

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值