是否可以在上下文配置中指定一个 @Component class 以便自动解析它的依赖关系?

Is it possible to specify one @Component class in context config so it's dependencies would be resolved automatically?

我想在我的 Spring 引导应用程序中执行单个服务的集成测试。所以我只想将此服务及其依赖项添加到测试应用程序上下文中。

假设我在不同的包 AB 中有两个 class(AB 上有一个引用)标记为 @Component 注释。当我 运行 应用程序时,@SpringBootApplication 找到我所有的 beans 并使用隐式 @ComponentScan.

将它们添加到上下文中

在我的测试配置中,我只想指定一个 class A 并使 Spring 启动以解决它的依赖项 (B) 并将其添加到上下文。

是否可以使用 Spring Boot 2 或最适合我的选项来创建一个单独的 @Configuration 并显式声明所需的 classes? (其实不止2个,所以才问)

In my test configuration I would like to specify only one class A and make Spring Boot to resolve it's dependencies (B) and add it to the context.

不,不幸的是,这是不可能的,因为 Spring 不知道如何找到依赖项 and/or 您真正想要的具体类型。

Is it possible with Spring Boot 2 or the most suitable option for me would to create a separate @Configuration with required classes declared explicitly?

是的,您可以声明专用于此目的的测试 @Configuration class;但是,您可能会发现在整个测试 classes 中简单地重用 common 组配置 classes 以便从上下文缓存支持中获益更为有益在 Spring TestContext 框架.