@EnableJpaRepositories tells Spring Data JPA where to look for repository interfaces and how to configure them, then registers a proxy bean for each one. In a Spring Boot application you usually do not need it: Boot auto-configures Spring Data JPA and scans the package of your @SpringBootApplication class. You add it when repositories live outside that package, or when you need custom settings such as several data sources.
Adding it on a Boot application switches off the auto-configured repository scanning, so its attributes then fully control what is found.
Spring Data JPA turns interfaces such as JpaRepository<User, Long> into working beans. @EnableJpaRepositories triggers that process: it scans the configured packages, creates a proxy for each repository interface, and wires it to an EntityManagerFactory and a transaction manager.
With spring-boot-starter-data-jpa on the classpath, JpaRepositoriesAutoConfiguration enables repositories automatically. It scans the package of the class annotated with @SpringBootApplication and its sub-packages. So this is enough:
Any repository under com.example.shop is picked up with no extra annotation. Entities are found the same way through the entity scan.
| Attribute | Purpose |
|---|---|
basePackages / value | Packages to scan for repository interfaces |
basePackageClasses | Type-safe alternative: scan the package of each listed class |
entityManagerFactoryRef | Bean name of the EntityManagerFactory to use, needed with several data sources |
transactionManagerRef | Bean name of the transaction manager for these repositories |
repositoryImplementationPostfix | Suffix for custom implementations, Impl by default |
includeFilters / excludeFilters | Fine-grained control over which interfaces are registered |
bootstrapMode | DEFAULT, LAZY or DEFERRED repository initialisation |
considerNestedRepositories | Whether nested repository interfaces are included |
Prefer basePackageClasses over string package names; it survives refactoring.
If your repositories and entities sit in a different root package, the default scan will not see them. Declare both scans.
@EntityScan is the entity counterpart. Forgetting it gives "Not a managed type" errors even when the repository is found.
Each data source needs its own @EnableJpaRepositories with explicit references, usually on separate configuration classes with separate packages.
Repeat for the second data source with its own packages. Repository packages must not overlap, or the same interface will be bound twice.
basePackages or basePackageClasses.@EnableJpaRepositories on a Boot app turns off the auto-configured scan, so list every package you need.@EntityScan or fix the persistence unit packages.entityManagerFactoryRef and transactionManagerRef explicitly.jakarta.persistence, not javax.persistence.Not in the normal case. Boot scans from the main application class package. Add it only for other packages or custom configuration.
The first registers repository beans. The second turns on auditing fields such as @CreatedDate. They are independent.
No. Each store has its own annotation, for example @EnableMongoRepositories.
On any @Configuration class, or on the main application class.
@EnableJpaRepositories registers Spring Data JPA repository interfaces as beans and lets you control scanning, the entity manager and the transaction manager. Spring Boot does this automatically for its own package tree, so use the annotation only for custom packages or multiple data sources, and pair it with @EntityScan when entities live elsewhere.