java - Spring Boot 组件扫描仅包含特定包

标签 java spring dependency-injection spring-boot component-scan

我有两个 Spring Boot项目让我们称他们为FooBar . Bar包含在 Foo 中作为依赖。现在我只想包含来自 Bar 的特定包在Component ScanFoo (比方说:com.example.bar.ctrl)。我知道您可以通过 excludeFilters 排除包裹但这需要更多的努力,而且我需要维护 exclude filter-list每次我在 Bar 中创建一个新包时. 记录该行为的最佳方式是什么?

最佳答案

我会这样做:

1) Foo 包中的所有 bean 都位于 com.example.foo 下,并且 @SpringBootApplication 主类也位于该包中,因此它会扫描所有 bean来自 com.example.foo 包。

2) 在 bar 包 com.example.bar.ctrl 中,我将创建带有 @ComponentScan 注释的 @Configuration 没有任何属性,所以它只会扫描 ctrl 包 + 子包。

3) 我会将点 2) 中的 @Import 配置类放入点 1) 中的主类中

@ComponentScanincludeexclude 属性不需要显式包。

关于java - Spring Boot 组件扫描仅包含特定包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38476807/

相关文章:

java - 如何使用 JDBC 将类存储到数据库中

java - 扫雷堆栈溢出错误

java - @Resource 注解的含义

Spring RabbitTemplate 未使用 TTL 创建死信队列

java - 被测单元 : Impl or Interface?

c# - 依赖注入(inject)有什么大的改进?

java - Eclipse Neon - 内容辅助超时

spring - 如何在 Ubuntu 服务器上安装 Spring Boot 应用程序?

java - 带有实体类的 NullPointerException

java - 使用配置播种 Dagger 2 Factory