Spring 引导组件扫描包括单个 class

Spring boot component scan include a single class

我正在使用 spring 组件扫描来自动检测 beans:

@ComponentScan({"com.org.x, com.org.y"})

问题是我想扫描 com.org.x 中的所有 classes,但我想从 [=] 中单独扫描一个 class、com.org.y.SomeService.class 15=]

我怎样才能做到这一点?

另外,除了使用上下文扫描,我如何创建这个 bean 并注入到应用程序上下文中?

您应该在配置 class 中使用带有 @Bean 注释的方法来定义您的 bean,如 the documentation 中所述。

@Import(com.org.y.SomeService.class) 适用于我的情况(即使 SomeService@Service,而不是 @Configuration