@Repository marks a class as a data access object (DAO) in Spring. It is a stereotype annotation, a specialisation of @Component, so Spring detects the class during component scanning and registers it as a bean. It also enables automatic translation of persistence exceptions into Spring's DataAccessException hierarchy.
If you use Spring Data interfaces, the annotation is optional on the interface because Spring Data creates the proxy for you. It matters mostly on hand-written repository classes.
The annotation has two practical effects:
@Repository is meta-annotated with @Component, classes carrying it are picked up by @ComponentScan (which @SpringBootApplication includes) and become beans.PersistenceExceptionTranslationPostProcessor (Spring Boot does this automatically) that wraps @Repository beans in a proxy. Exceptions such as SQLException or jakarta.persistence.PersistenceException are converted into unchecked subclasses of DataAccessException, for example DuplicateKeyException or EmptyResultDataAccessException.It also documents the role of the class: it belongs to the persistence layer and should hold no business logic.
| Annotation | Layer | Extra behaviour |
|---|---|---|
@Component | Any | Generic bean detection |
@Service | Business logic | Semantic only |
@Repository | Persistence | Exception translation |
@Controller | Web | Request mapping support |
All four register a bean. Choosing the right one makes the architecture readable and lets AOP pointcuts target a layer.
JdbcClient is available since Spring Framework 6.1. A duplicate key during insert surfaces as DuplicateKeyException, not as a raw SQLException, thanks to the translation described above. Callers can handle it without importing any JDBC class.
With JPA you inject an EntityManager and the exception translation covers PersistenceException too:
Transactions belong on the service layer with @Transactional, not on the repository.
For most projects you declare an interface and let Spring Data generate the implementation:
No @Repository is required here. Spring Data detects interfaces extending Repository through @EnableJpaRepositories (applied automatically by Boot), and the generated proxy already translates exceptions. Adding the annotation is harmless and some teams keep it for readability, but it is redundant.
@Service.SQLException from a @Repository bean. It never arrives, because it has been translated.new instead of injecting it, which bypasses the proxy and the exception translation.@Repository outside the scanned packages, so no bean is created.final on repositories if translation does not apply.No. Spring Data registers the interfaces on its own. The annotation only matters for classes you write yourself.
Both register a bean. @Repository additionally enables persistence exception translation and states the persistence role.
Exceptions from JDBC, JPA, Hibernate and other supported persistence technologies become subclasses of DataAccessException, which is unchecked.
You can, but you should not. Use @Service for business logic so the roles stay clear.
@Repository is a stereotype for persistence-layer classes. It makes Spring register the bean and translate persistence exceptions to DataAccessException. Use it on hand-written DAOs; Spring Data interfaces do not need it.