In Spring, @Repository marks a class as part of the data access layer. It makes the class a scanned Spring bean and enables translation of persistence exceptions into Spring's DataAccessException hierarchy. With Spring Data JPA the annotation is optional on repository interfaces, because Spring Data detects interfaces that extend Repository and applies exception translation to the generated proxy on its own.
This article focuses on how @Repository relates to Spring Data JPA, which is where most developers meet it today. Spring Data does not use the annotation to decide whether to create an implementation.
@Repository is a stereotype annotation in Spring Framework core, meta-annotated with @Component. Component scanning registers annotated classes as beans, and a PersistenceExceptionTranslationPostProcessor wraps them so that exceptions such as SQLException or JPA's PersistenceException surface as unchecked subclasses of DataAccessException, for example DataIntegrityViolationException. The annotation is also a clear signal to other developers that the class touches the database.
A Spring Data JPA repository is an interface. Spring Data creates a proxy implementation at startup, based on the interface, and registers it as a bean:
No @Repository is needed. The proxy exists because the interface extends JpaRepository (which extends Repository) and lies in a package covered by repository scanning. With Spring Boot, auto-configuration enables scanning for the package of your @SpringBootApplication class; outside Boot you use @EnableJpaRepositories. Exception translation is applied by Spring Data's repository factory, so it works without the annotation too.
Adding @Repository to such an interface is harmless and some teams keep it for clarity, but it is not what creates the implementation. A claim that Spring Data creates a proxy because of @Repository is a common misconception.
The entity used above:
Spring Boot 3 and later use the jakarta.persistence package; older Boot 2.x code uses javax.persistence.
The derived query findByDepartment becomes SQL such as select ... from employee where department = ?. Custom queries use @Query on a method; they need no @Repository either.
Use the annotation on classes you implement yourself:
EntityManager, JdbcTemplate or another client.EmployeeRepositoryImpl, which is picked up by naming convention and benefits from translation.Then public interface EmployeeRepository extends JpaRepository<Employee, Long>, EmployeeSearch {} merges the custom method into the generated repository.
| Situation | Need @Repository? |
|---|---|
| Spring Data interface | No, optional |
Custom class using EntityManager | Yes, for translation |
Class using JdbcTemplate or JdbcClient | Not for translation, recommended for clarity |
| Service or controller | No, use the matching stereotype |
@DataJpaTest boots only JPA components and an embedded database by default:
No. Spring Data finds the interface through repository scanning and registers the proxy bean. The annotation is optional.
No. Transactions come from @Transactional. Spring Data repository methods are transactional by default (read-only for queries), but your service methods should define the business transaction boundaries.
@Repository is for data access and enables exception translation. @Service marks business logic and adds no behaviour; it states intent.
Yes. The same translation mechanism applies, and repository interfaces for those modules are also detected through scanning without the annotation.
@Repository registers a data access class as a bean and enables exception translation. With Spring Data JPA you declare interfaces, and Spring Data creates the implementation from the Repository hierarchy and scanning, not from the annotation. Keep @Repository for hand-written DAOs and custom repository implementations, use @Transactional in services, and test the layer with @DataJpaTest.