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

时间:2016-06-10 22:07:16

标签: java spring spring-boot spring-annotations component-scan

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

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

问题是我希望扫描com.org.x中的所有课程,但我想要从com.org.y.SomeService.class

单独扫描一个课程com.org.y

我怎样才能做到这一点?

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

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:2)

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