SpringBoot scanBasePackages在多模块项目中不起作用

时间:2018-07-18 14:43:01

标签: spring-boot

我具有以下Maven项目结构:

eu.arrowhead
  common
    repository
       -AJpaRepository.class
  orchestrator
    controller
       -AController.class
    OrchestratorApplication
  other_modules...

其中两个模块是commonorchestrator。 Common是Orchestrator模块的依赖项。 JpaRepositoryClass带有@Repository注释。

在控制器类中,我使用构造函数自动装配来获取存储库的副本:

private final AJpaRepository serviceRepo;

  @Autowired
  public AController(AJpaRepository serviceRepo){
    this.serviceRepo = serviceRepo;
  }

最后,在Application类中,我使用scanBasePackages从通用模块中提取组件:

@SpringBootApplication(scanBasePackages = "eu.arrowhead")
public class OrchestratorApplication {

  public static void main(String[] args) {
    SpringApplication.run(OrchestratorApplication.class, args);
  }

}

启动应用程序时,我得到:

Description:

Parameter 0 of constructor in eu.arrowhead.orchestrator.controller.ArrowheadServiceController required a bean of type 'eu.arrowhead.common.repository.ArrowheadServiceRepo' that could not be found.


Action:

Consider defining a bean of type 'eu.arrowhead.common.repository.ArrowheadServiceRepo' in your configuration.

如果我使用scanBasePackages = {"eu.arrowhead.common"},则应用程序启动时不会出现错误,但是我无法到达控制器类中的端点(获取默认404错误)。如果我写scanBasePackages = {"eu.arrowhead.common", "eu.arrowhead.orchestrator"}就像只有“ eu.arrowhead”一样,在启动时会遇到相同的错误。

这应该如何工作?我对此表示高度怀疑。

后裔:

  • 常用模块:starter-data-jpa,starter-json,mysql-connector-java,hibernate-validator
  • Orch模块:starter-web,通用模块。

我也尝试使用@ComponentScan,但结果相同。问题是什么?谢谢。

1 个答案:

答案 0 :(得分:2)

您缺少@EnableJpaRepositories("eu.arrowhead")注释,无法启用Spring Data JPA存储库扫描。