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 @Controller and @Service Annotation in Spring

In the Spring framework, @Controller and @Service are both stereotype annotations that allow the Spring container to detect and register beans automatically. While they both serve this primary purpose, they have distinct roles and usages in a typical Spring application, which denote different layers or components of an application.

Here's a breakdown of their differences:

  1. @Controller:

    • Primary Purpose: This annotation marks a class as a web-layer component, specifically a Spring MVC controller.
    • Role in MVC: It represents the Controller part in the Model-View-Controller (MVC) pattern. Such classes are responsible for processing incoming HTTP requests, invoking business logic, returning responses, and (in traditional web apps) choosing which view should be displayed.
    • Typical Operations:
      • Handle HTTP requests (e.g., GET, POST).
      • Invoke services or business logic.
      • Return a view name or data as a response.
    • Commonly Used With: Other annotations like @RequestMapping, @GetMapping, @PostMapping, etc., to define routing information.
    • Usage Example:
      @Controller
      public class UserController {
          @Autowired
          private UserService userService;
      
          @GetMapping("/users")
          public String listUsers(Model model) {
              model.addAttribute("users", userService.findAll());
              return "userList";
          }
      }
      
  2. @Service:

    • Primary Purpose: It's used to annotate service-layer classes, which contain business logic or call other backend services.
    • Role in Application: @Service beans are typically used to implement core functionality like computations, database operations, invoking APIs, etc. They don't handle HTTP requests directly but might be invoked by controllers.
    • Typical Operations:
      • Execute business logic.
      • Interact with databases or other services.
      • Can be transactional (using @Transactional).
    • Usage Example:
      @Service
      public class UserService {
          @Autowired
          private UserRepository userRepository;
      
          public List<User> findAll() {
              return userRepository.findAll();
          }
      }
      

Key Takeaway:

  • @Controller is oriented toward processing HTTP requests and responses, serving as a bridge between the user and the application's backend.

  • @Service, on the other hand, is all about implementing the application's business logic or interfacing with databases and other services.

In a layered application architecture, controllers rely on services to execute the required operations and return results. Controllers should ideally be lightweight and delegate most of the heavy lifting to services or other components. The separation ensures a clear distinction of responsibilities, making the codebase more maintainable and testable.

  1. Examples of @Controller and @Service in a Spring application:

    • Example of @Controller:
      @Controller
      public class MyController {
          @GetMapping("/hello")
          public String sayHello() {
              return "hello";
          }
      }
      
    • Example of @Service:
      @Service
      public class MyService {
          public String performBusinessLogic() {
              // Business logic
              return "result";
          }
      }
      

    In this example, @Controller handles a web request, while @Service encapsulates business logic.