Home > Back-end >  Spring - import jpa repositories from another module
Spring - import jpa repositories from another module

Time:02-10

I have two gradle modules: common and app

The common module contains a JpaRepository named OutboxJpaMessageRepository.

The app module contains another JpaRepository named RentRepository.

The common has the following configuration class, which is auto-imported by the other module (using a spring.factories file):

@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).

Can a library exports jpa repositories without override the default configuration of the client?

I don't want to re-configure all apps which use the common module

CodePudding user response:

Two solutions I can think of:

  • 1: @EnableJpaRepositories accepts an array as parameter, pass it path.to.app.module.
  • 2: Use @EnableAutoConfiguration and spare yourself a lot of trouble.

CodePudding user response:

The boot module must contain multi path lick this:

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

  • Related