@EnableJpaRepositories tells Spring Data JPA where to find repository interfaces and how to configure them. Spring creates a proxy implementation for each interface that extends JpaRepository or CrudRepository. In a Spring Boot application you usually do not need it, because auto-configuration already enables repositories for the package of your @SpringBootApplication class. You add it when you must customize scanning, use several data sources, or supply a custom base class.
Using it incorrectly is a common cause of "No qualifying bean of type" errors, so it helps to know what Boot does for you and when your annotation replaces that behavior.
When spring-boot-starter-data-jpa is on the classpath, JpaRepositoriesAutoConfiguration scans the package of the main application class and its sub-packages and registers a bean for each repository interface. The auto-configuration backs off as soon as you declare your own @EnableJpaRepositories or a JpaRepositoryFactoryBean.
With this class alone, a repository in com.example.shop.product is found automatically. Adding @EnableJpaRepositories with no attributes to a Boot application is redundant, and adding it with basePackages also turns off the default scan, so entities and repositories outside the listed packages stop working.
You write no implementation. Spring Data derives the query from the method name and creates a proxy at startup. On Spring Boot 3 and later, entities use the jakarta.persistence package; older code with javax.persistence belongs to Boot 2.
| Attribute | Purpose |
|---|---|
basePackages / value | Packages scanned for repositories |
basePackageClasses | Type-safe alternative to package names |
entityManagerFactoryRef | Bean name of the EntityManagerFactory to use |
transactionManagerRef | Bean name of the transaction manager |
repositoryBaseClass | Custom base class for all repositories |
repositoryImplementationPostfix | Suffix for custom fragments, default Impl |
includeFilters / excludeFilters | Fine control over what is picked up |
bootstrapMode | DEFAULT, LAZY or DEFERRED initialization |
enableDefaultTransactions | Whether CRUD methods get a default transaction |
The classic reason to write the annotation yourself is two databases, each with its own packages and entity manager.
Create a second configuration class with its own packages and bean names for the other database, and mark one data source as @Primary if other code injects an unqualified one. Each @EnableJpaRepositories must point at non-overlapping packages.
To add a method to every repository, supply a base class and register it.
Then use @EnableJpaRepositories(repositoryBaseClass = AuditedRepository.class). For one repository only, prefer a custom fragment interface with an Impl class instead of a global base class.
basePackages.@EntityScan controls entities, separately from repository scanning.bootstrapMode = BootstrapMode.DEFERRED to initialize repositories on a background thread.@EnableJpaRepositories in Spring Boot?Usually not. Auto-configuration enables repositories in the application class package; add the annotation only to change scanning or configuration.
@EnableJpaRepositories and @EntityScan?The first finds repository interfaces, the second finds @Entity classes. Moving one does not move the other.
Yes. In plain Spring, it is required on a configuration class, together with an EntityManagerFactory and a transaction manager.
No. Each store has its own annotation, for example @EnableMongoRepositories.
@EnableJpaRepositories registers Spring Data JPA repositories and lets you control packages, entity manager, transaction manager and base class. Spring Boot already enables it by default, so add it only for multiple data sources or custom setup, and remember that declaring it disables the default scan.