@Repository is a Spring stereotype annotation that marks a class as a data access object (DAO) in the persistence layer. It does two practical things: it registers the class as a Spring bean during component scanning, and it makes Spring translate persistence-technology exceptions such as SQLException or PersistenceException into Spring's unchecked DataAccessException hierarchy. It also documents the role of the class for readers and for tools such as AOP pointcuts.
@Repository belongs to Spring Framework core (org.springframework.stereotype), not to Spring Data. It is itself meta-annotated with @Component.
Because @Repository is a specialization of @Component, classes carrying it are found by component scanning (@SpringBootApplication or @ComponentScan) and registered as singleton beans:
Any other bean can then inject ProductDao. You can give the bean a name with @Repository("products"), but the default name (the decapitalized class name) is almost always fine.
The stereotypes are technically interchangeable for scanning, and each states a layer:
| Annotation | Layer | Extra behaviour |
|---|---|---|
@Component | Any | None |
@Repository | Persistence | Exception translation |
@Service | Business logic | None today, marks intent |
@Controller / @RestController | Web | Request mapping detection |
Persistence APIs throw their own exceptions: JDBC throws checked SQLException, JPA throws PersistenceException, and Hibernate has its own hierarchy. If your service layer catches those, it depends on the persistence technology. Spring defines a technology-neutral, unchecked hierarchy rooted in DataAccessException, with subclasses such as DuplicateKeyException, DataIntegrityViolationException and EmptyResultDataAccessException.
@Repository opts a class into automatic translation. A PersistenceExceptionTranslationPostProcessor finds beans annotated with @Repository and wraps them in a proxy that catches runtime persistence exceptions and rethrows the translated Spring exception. Spring Boot registers this post-processor automatically; in plain Spring you declare it as a bean.
Here is a DAO that uses the JPA EntityManager directly:
If persist violates a unique constraint, the caller does not see a Hibernate or JDBC exception. It sees a DataIntegrityViolationException, which the service can handle without importing any JPA classes:
Note that with JPA, the constraint violation may only surface at flush or commit time, not on the persist call, so the exception may appear when the transaction ends rather than inside the try block.
@Repository is not required for exception translation in every case:
JdbcTemplate, JdbcClient and NamedParameterJdbcTemplate already translate exceptions themselves, so a DAO built on them works with or without the annotation. Keeping @Repository is still good practice for scanning and clarity.@Repository in Spring Data JPA for details.A JDBC example:
JdbcClient is available from Spring Framework 6.1.
DataAccessException in the DAO; let the service decide and handle the transaction.@DataJpaTest load repositories without the web layer.Both register a bean. @Repository additionally enables persistence exception translation and states that the class is in the data access layer.
No. It is in Spring Framework core. Spring Data builds on the same translation mechanism but discovers repository interfaces on its own.
You can, but Spring Data does not need it; it detects interfaces extending Repository through repository scanning. On a plain interface without Spring Data, the annotation has no effect because no implementation exists.
The class may still be a bean if you register it another way, but persistence exceptions will not be translated, so callers get raw JPA or Hibernate exceptions.
@Repository marks a persistence-layer class, registers it through component scanning, and enables translation of vendor-specific persistence exceptions to Spring's DataAccessException hierarchy. It is part of Spring Framework core and a specialization of @Component. Use it on DAOs that work with EntityManager, JDBC or custom stores, and remember that Spring Data interfaces and JDBC templates translate exceptions on their own.