Simplifying Component Definition with Spring Stereotype Annotations

Manpreet Singh
3 min readMar 29

Stereotype annotations are a type of annotations in the Spring Framework that are used to simplify the process of configuring and defining components in a Spring application. Stereotype annotations are a set of pre-defined annotations that can be used to mark classes as specific types of components, such as controllers, services, repositories, and others. In this article, we will explore the various stereotype annotations provided by the Spring Framework and their usage.

  1. @Controller

The @Controller annotation is used to mark a class as a Spring MVC controller. This annotation indicates that the class is responsible for handling HTTP requests and producing HTTP responses. When this annotation is used, the Spring container will create an instance of the class and register it as a controller in the web application context.

@Controller
public class MyController {
// controller methods
}

2. @Service

The @Service annotation is used to mark a class as a service in a Spring application. This annotation indicates that the class performs some business logic operations and is used by the controllers to process user requests. When this annotation is used, the Spring container will create an instance of the class and register it as a service in the application context.

@Service
public class MyService {
// service methods
}

3. @Repository

The @Repository annotation is used to mark a class as a repository in a Spring application. This annotation indicates that the class is responsible for storing and retrieving data from a database or some other data store. When this annotation is used, the Spring container will create an instance of the class and register it as a repository in the application context.

@Repository
public class MyRepository {
// repository methods
}

4. @Component

The @Component annotation is a generic stereotype annotation used to mark a class as a Spring component. This annotation indicates that the class is a generic Spring-managed component and can be used for any purpose. When this annotation is used, the Spring container will create an instance of the class and register it as a component in the application context.

@Component
public class MyComponent {
// component methods
}
Manpreet Singh

Software developer who loves writing about programming, technology, passive income strategies etc.