Spring - import jpa repositories from another module

Viewed 833

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

3 Answers

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.

The boot module must contain multi path lick this:

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

I found the solution to my problem.

I solved using @AutoConfigurationPackage

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