Spring启动组件扫描包括单个类

Ana*_*man 7 java spring spring-annotations spring-boot component-scan

我使用spring组件扫描来自动检测bean:

@ComponentScan({"com.org.x, com.org.y"})
Run Code Online (Sandbox Code Playgroud)

问题是我想要com.org.x扫描所有类,但我想要一个单独的类com.org.y.SomeService.class,单独扫描com.org.y

我怎样才能做到这一点?

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

Lu5*_*u55 5

@Import(com.org.y.SomeService.class)就我而言适用(即使SomeService@Service,而不是@Configuration


JB *_*zet 3

@Bean您应该使用配置类中注释的方法来定义您的 bean ,如文档中所述。