What is the use of @EnableJpaRepositories method?

@EnableJpaRepositories is a configuration-class annotation that turns on Spring Data JPA's repository support, telling Spring which package(s) to scan for repository interfaces so it can generate their implementations automatically at startup.

Key Points: • It's placed on a @Configuration class, typically specifying basePackages if repositories live outside the component-scanned default package. • It registers the infrastructure beans (like the repository factory) needed to turn interfaces like JpaRepository into working Spring beans. • Spring Boot auto-configuration usually enables this automatically based on the main application class's package, so most Spring Boot apps never need to declare it explicitly. • It becomes necessary to declare explicitly in multi-module projects or when repositories live in a package the default component scan wouldn't reach.

Example: In a multi-module Spring application where repository interfaces live in a separate library module outside the main application's package tree, @EnableJpaRepositories(basePackages = "com.company.repository") tells Spring exactly where to look for them.

Code Example:

@Configuration
@EnableJpaRepositories(basePackages = "com.company.repository")
public class JpaConfig {
}

Interview Tip: A concise interview answer is:

"@EnableJpaRepositories tells Spring which packages to scan for repository interfaces so it can generate their implementations at startup. Spring Boot auto-configures this for you in the common case, so I only need to declare it explicitly when repositories live outside the default component-scanned package, like in a multi-module project."