I have a multi-module Maven project using Spring Boot 3.4.4 with the following structure:
my-project/
├── pom.xml
├── lib-model/
│ ├── pom.xml
│ ├── src/main/java/com/example/libmodel/
│ │ ├── Model.java
│ │ ├── ModelRepository.java
│ │ ├── ModelController.java
│ │ └── LibModelAutoConfiguration.java
│ └── src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
├── app-main/
│ ├── pom.xml
│ ├── src/main/java/com/example/app/
│ │ ├── AppMainApplication.java
│ │ ├── Other.java
│ │ ├── OtherRepository.java
│ │ └── OtherController.java
│ └── src/main/resources/application.properties
The library lib-model is intended to be plug-and-play, auto-configuring its own @Entity, @Repository, and @RestController. It uses Spring Boot’s auto-configuration mechanism:
// com.example.libmodel.LibModelAutoConfiguration
@Configuration
@EntityScan(basePackages = "com.example.libmodel")
@EnableJpaRepositories(basePackages = "com.example.libmodel")
public class LibModelAutoConfiguration {
}
And is declared in META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports:
com.example.libmodel.LibModelAutoConfiguration
The main app (app-main) has its own entities and repositories, all under com.example.app.
The Problem
When I add lib-model as a dependency, Spring Boot no longer detects the repositories in my application module:
Parameter 0 of constructor in com.example.app.OtherController required a bean of type 'com.example.app.OtherRepository' that could not be found.
If I manually add:
@EnableJpaRepositories(basePackages = "com.example.app")
@EntityScan(basePackages = "com.example.app")
...to the main application class, it works again — but I would like to avoid this, since it was working fine before adding the library.
What I Think Is Happening
It seems that @EnableJpaRepositories in the library's @Configuration class overrides Spring Boot's default behavior, preventing it from scanning the main application package for repositories.
But I want the library to only configure its own stuff, and not interfere with the application’s own configuration.
Desired Behavior
- I want the library to auto-configure only its own entities and repositories.
- I want the application to continue auto-detecting its own repositories/entities without needing explicit annotations.
Question
Is there a way to isolate the @EnableJpaRepositories (and @EntityScan) in the library module so that it only applies to itself, and doesn’t break repository scanning in the main application?