'Spring - import jpa repositories from another module

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



Solution 1:[1]

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.

Solution 2:[2]

The boot module must contain multi path lick this:

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

Solution 3:[3]

I found the solution to my problem.

I solved using @AutoConfigurationPackage

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 JavaProScript
Solution 2 Knewbit
Solution 3 Vincenzo