Junior — Middle
In which cases is the @Repository annotation used when implementing inheritance in Spring?
sobes.tech AI
Answer from AI
The @Repository annotation in Spring is used to designate components responsible for data access (DAO — Data Access Object). When implementing inheritance, it is used for:
- Marking a class as a Spring bean involved in transaction management and exception handling related to the database.
- Automatic conversion of ORM or JDBC-specific exceptions into unified Spring exceptions (DataAccessException).
If you create a class that inherits from a base repository or implements a repository interface, the @Repository annotation helps Spring correctly recognize this class as a data access component and apply the appropriate aspects.
Example:
@Repository
public class UserRepositoryImpl extends BaseRepository implements UserRepository {
// Implementation of data access methods
}