🌱 Spring Boot Q2 / 50

What is the difference between @Component, @Service, @Repository, and @Controller in Spring Boot?

AI-Powered Answer ✓ Answered

In Spring Boot, `@Component`, `@Service`, `@Repository`, and `@Controller` are stereotype annotations that mark a class as a Spring component. While they all ultimately extend `@Component`, they serve distinct purposes, improving code readability, maintainability, and providing specific functionalities.

@Component

This is the most generic stereotype annotation. It marks a class as a Spring-managed component. Spring's component scanning mechanism automatically detects classes annotated with @Component and registers them as beans in the ApplicationContext. It's used for general-purpose components that don't fit into other specific layers.

java
@Component
public class MyUtilityComponent {
    // ... utility methods
}

@Service

The @Service annotation is used to mark a class that contains business logic. It's typically applied to classes in the service layer, which orchestrate operations between the web layer and the data access layer. While technically a specialization of @Component, its semantic meaning helps in structuring the application logically.

java
@Service
public class ProductService {
    // ... business logic for products
}

@Repository

The @Repository annotation is used for classes that interact directly with a database or other data sources. It is typically applied to Data Access Object (DAO) classes. Beyond semantic clarity, @Repository enables Spring's JDBC exception translation, converting data access-specific exceptions into a consistent hierarchy of Spring's DataAccessExceptions.

java
@Repository
public class ProductRepository {
    // ... methods for database interaction
}

@Controller

The @Controller annotation is used to mark classes that handle incoming web requests. These classes are part of the presentation layer in a Spring MVC application. They typically contain methods mapped to URLs, processing user input, and returning views or data. For RESTful services, @RestController is a convenient stereotype that combines @Controller and @ResponseBody.

java
@Controller
public class ProductController {
    // ... methods to handle web requests
}

Key Differences and Why They Matter

While all these annotations make a class eligible for component scanning and dependency injection, their specialized roles provide more than just semantic value. They enhance application structure, enable specific Spring features, and improve readability.

  • Semantic Clarity: Each annotation clearly defines the role of a component within the application's architecture (e.g., business logic, data access, web request handling).
  • Exception Translation: @Repository specifically provides automatic translation of persistent layer exceptions (e.g., Hibernate, JDBC) into Spring's unified DataAccessException hierarchy.
  • Specific Features: @Controller and @RestController are critical for Spring MVC's request handling capabilities, enabling features like request mapping, model binding, and view resolution.
  • Future Enhancements: Spring and other frameworks can potentially add specific functionalities or apply aspects to classes based on these stereotype annotations in future versions.
AnnotationPurpose/LayerSpecial Functionality
@ComponentGeneric Spring componentNone specific beyond being a component
@ServiceBusiness logic layerNone specific; purely semantic
@RepositoryData access layer (DAO)Enables Spring's DataAccessException translation
@ControllerWeb layer (MVC)Handles web requests; enables request mapping, view resolution