简体   繁体   English

Spring - 从另一个模块导入 jpa 个存储库

[英]Spring - import jpa repositories from another module

I have two gradle modules: common and app我有两个 gradle 模块: commonapp

The common module contains a JpaRepository named OutboxJpaMessageRepository . common模块包含一个名为 OutboxJpaMessageRepository 的OutboxJpaMessageRepository

The app module contains another JpaRepository named RentRepository . app模块包含另一个名为 RentRepository 的RentRepository

The common has the following configuration class, which is auto-imported by the other module (using a spring.factories file): common具有以下配置 class,它由其他模块自动导入(使用spring.factories文件):

@Configuration
@EntityScan("path.to.common.module")
@EnableJpaRepositories("path.to.common.module")
@ComponentScan("path.to.common.module")
public class OutboxMessagingAutoConfig {
}

The problem is that @EnableJpaRepositories("path.to.common.module") overrides the default auto-configuration of the application, and the app can't find RentRepository (different package).问题是@EnableJpaRepositories("path.to.common.module")覆盖了应用程序的默认自动配置,并且应用程序找不到RentRepository (不同的包)。

Can a library exports jpa repositories without override the default configuration of the client?库可以导出 jpa 个存储库而不覆盖客户端的默认配置吗?

I don't want to re-configure all apps which use the common module我不想重新配置所有使用公共模块的应用程序

Two solutions I can think of:我能想到的两种解决方案:

  • 1: @EnableJpaRepositories accepts an array as parameter, pass it path.to.app.module. 1:@EnableJpaRepositories 接受一个数组作为参数,将它传递给path.to.app.module。
  • 2: Use @EnableAutoConfiguration and spare yourself a lot of trouble. 2:使用@EnableAutoConfiguration,省去很多麻烦。

The boot module must contain multi path lick this: boot模块必须包含多路径舔这个:

@ComponentScan({"path.to.common.module", "path.to.app.module"}) @ComponentScan({"path.to.common.module", "path.to.app.module"})

I found the solution to my problem.我找到了解决问题的办法。

I solved using @AutoConfigurationPackage我解决了使用@AutoConfigurationPackage

@Configuration
@AutoConfigurationPackage
@ComponentScan("path.to.common.module")
public class OutboxJpaMessagingConfig {
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM