Understanding Spring Stereotype Annotations

Eric Anicet
3 min readNov 7, 2024

Spring Framework provides stereotyped annotations, which indicate a class’s role in the context of the application. These annotations are located in the org.springframework.stereotypepackage. When we use the stereotype annotations, Spring automatically imports the annotated classes as beans into the application context and injects them into dependencies.

Spring Stereotypes

@Component

The @Component annotation marks Java classes as candidates for automatic detection, which means that Spring will automatically create an instance of this class and manage its lifecycle.

@Component
public class BookCache {
...
}

@Repository

Indicates that an annotated class is a “Repository”, originally defined by Domain-Driven Design (Evans, 2003) as “a mechanism for encapsulating storage, retrieval, and search behavior which emulates a collection of objects”. This means that it marks a class as a repository, which encapsulates the logic required to access data from a data source.

By annotating a class with @Repository, the class is eligible for the database-related exception translation mechanism in Spring DataAccessException.

@Repository
public interface BookRepository extends…

--

--