相关文章推荐
睡不着的楼房  ·  spring.main.allow-bean ...·  3 周前    · 
文武双全的小熊猫  ·  设置屏幕分辨率·  2 年前    · 
鬼畜的充值卡  ·  vue.js - elementUI ...·  2 年前    · 
闯红灯的煎鸡蛋  ·  react ...·  2 年前    · 

在 Spring Boot 中,可以通过在 @ComponentScan 注解中指定排除或包含特定的包或类来控制哪些 bean 应该被加载。下面是一些示例代码:

  1. 排除特定的包:

@SpringBootApplication
@ComponentScan(excludeFilters = @ComponentScan.Filter(type = FilterType.REGEX, pattern = "com.example.exclude.*"))
public class MyApplication {
// Application code here...
}

在这个示例中,@ComponentScan 注解中的 excludeFilters 属性使用正则表达式匹配并排除了 com.example.exclude 包及其子包中的所有组件。

2. 包含特定的包:

@SpringBootApplication
@ComponentScan(basePackages = { "com.example.include" })
public class MyApplication {
// Application code here...
}
在这个示例中,@ComponentScan 注解中的 basePackages 属性指定只扫描 com.example.include 包及其子包中的组件。

需要注意的是,如果同时使用了 excludeFilters 和 includeFilters 属性,那么 excludeFilters 属性会优先于 includeFilters 属性生效。因此,如果想要精确地控制哪些 bean 应该被加载,最好只使用其中一个属性。