Spring MVC Tutorial

Core Spring MVC

Spring MVC - Annotation

Spring MVC - Form Handling

Spring MVC with JSTL

Spring MVC with REST API

Spring MVC with Database

Difference Between @Component, @Repository, @Service, and @Controller Annotations in Spring

In Spring Framework, @Component, @Repository, @Service, and @Controller are annotations used to indicate that a class should be automatically detected and registered as a bean in the Spring container. Although they all serve the same primary purpose (auto-detection of beans), they have different semantic meanings and sometimes come with added special functionalities.

Let's break them down:

  1. @Component:

    • Primary Purpose: General-purpose stereotype annotation to indicate that a class is a Spring component.
    • Use Cases: When your class doesn't fit into the more specific roles of @Repository, @Service, or @Controller.
    • Extra Features: No special technical features beyond marking a class for autodetection.
  2. @Repository:

    • Primary Purpose: Marks a class as a Data Access Object (DAO) component. Typically used with classes that access the database.
    • Use Cases: DAO classes that encapsulate access to data sources.
    • Extra Features:
      • Can be integrated with the persistence exception translation mechanism, converting platform-specific exceptions into Spring's DataAccessException.
  3. @Service:

    • Primary Purpose: Marks a class as a Service component. Typically used with service-layer classes where business logic is executed.
    • Use Cases: Business services, facade classes, helper services, etc.
    • Extra Features: No special technical features, but it indicates the specific role of the class.
  4. @Controller:

    • Primary Purpose: Marks a class as a web layer component in Spring MVC. It is a specialization of @Component.
    • Use Cases: Spring MVC controller classes that handle HTTP requests.
    • Extra Features:
      • The @Controller beans can make use of other annotations like @RequestMapping, @GetMapping, @PostMapping, etc., to map web requests to specific methods.

Things to Keep in Mind:

  • These annotations also assist with clear layering in the application architecture, indicating clear roles and responsibilities for classes. For example, separating concerns via a controller, service, repository pattern.

  • All these annotations are essentially specializations of @Component. Hence, classes annotated with any of these annotations will be auto-detected during classpath scanning.

  • Starting with Spring 4.x, there's also @RestController which is a combination of @Controller and @ResponseBody. This means the response returned by methods in a @RestController class doesn't need to be a view (like a JSP or Thymeleaf template); it can be a domain object, and Spring will handle converting it to JSON or XML, depending on the request's Accept header.

In summary, while these annotations technically accomplish the same thing (bean registration and autodetection), they provide semantic clarity about the role of a class in the application and, in some cases, come with additional features tailored to that role.

  1. Examples of @Component, @Repository, @Service, @Controller in Spring:

    • Here's a simple example to illustrate the usage:
    // @Component
    @Component
    public class MyComponent {
        // General-purpose bean
    }
    
    // @Repository
    @Repository
    public class MyRepository {
        // Data access bean
    }
    
    // @Service
    @Service
    public class MyService {
        // Service layer bean
    }
    
    // @Controller
    @Controller
    public class MyController {
        // Web layer bean
    }
    

    In a Spring application, you would use these annotations based on the role of each class.