spring 子模块 bean 未加载

spring submodule bean not loading

我在 test-client 模块中有 DemoService class。从 test-server 模块注入此 DemoService class。

第一种情况(工作):

test-servertest-client 都有根包作为 com.demo.test

这种情况下所有 bean 都在加载。

第二种情况:(组件扫描后工作)

test-server 的根包为 com.demo.test

test-client 的根包为 com.client.test

这种情况 test-client bean 没有加载。预计 ComponentScan.

第三种情况:(即使在组件扫描后仍无法正常工作)

test-client 模块使用来自 test-server 的 bean 之一。这也不会在 @ComponentScan 之后加载。

如何克服以上情况?

注意: 我在 build.gradle

中导入了子模块

implementation project(':test-client')

是的,如果 test-client 不共享相同的基础包(由您的 @SpringBootApplication class 所在的包定义),那么您将需要使用 ComponentScan 在你的 test-server 项目中:

@ComponentScan(basePackages = "com.client.test")

一个很好的选择是我们为客户端代码创建您自己的 Spring 启动启动项目,如 section "Creating Your Own Auto-configuration" in the official Spring Boot documentation 中所述。