In this tutorial, we will see differences between @Component, @Service, @Repository and @Controller annotations:

  • Spring provides further stereotype annotations: @Component, @Service, and @Controller.
  • @Component is a generic stereotype for any Spring-managed component.
  • @Repository, @Service, and @Controller are specializations of @Component for more specific use cases,.

In Spring, the @Component, @Repository, and @Service annotations are used to indicate that a class is a component of the application and should be managed by the Spring framework. These annotations are used to define the different layers of the application, and are part of the Spring Core framework.

@Component

  • @Component is a general-purpose annotation that is used to indicate that a class is a component of the application and should be managed by the Spring framework. It’s a more generic annotation that can be used in any layer of the application.
  • A class annotated with @Component is eligible for dependency injection by the Spring framework, which means that it can be easily reused and tested. The main purpose of this annotation is to indicate that a class is a component of the application and should be managed by the Spring framework, but it does not provide any additional functionality or context compared to other annotations like @Service, @Repository, or @Controller.
  • The @Component annotation can be used to mark any class as a Spring bean, regardless of whether it’s a service, repository, controller or any other type of class. It’s useful when you have a class that doesn’t fit into any of the other categories, but still needs to be managed by the Spring framework.
  • It’s also important to note that @Component classes can be used in conjunction with other Spring annotations such as @Autowired, @Transactional, and @Cacheable to handle transactions and caching.

@Repository

  • @Repository is a Spring annotation that is used to indicate that a class is a data repository and should be managed by the Spring framework. It’s used to indicate that a class is a data access object (DAO) and can be used to catch and translate data access exceptions.
  • A repository class is typically used in the data access layer of the application and it’s responsible for interacting with a data source, such as a database, to retrieve and persist data. Repository classes provide a consistent, well-defined API for the rest of the application to use when working with data.
  • The @Repository annotation is used to mark a class as a data repository and make it eligible for dependency injection. This allows the class to be easily reused and tested.
  • A repository class typically contains methods that perform CRUD (create, read, update, delete) operations on the data source and these methods can be called by other components of the application such as controllers, services, and other repository classes.
  • It’s also important to note that @Repository classes are typically used in conjunction with other Spring annotations such as @Autowired, @Transactional, and @Query to handle transactions, and to perform more complex queries on the data source.

@Service

  • @Service is a Spring annotation that is used to indicate that a class is a service component and should be managed by the Spring framework. It’s used to indicate that a class is a business service and it’s a common practice to use it in the service layer of the application.
  • A service class is typically used in the service layer of the application and it’s responsible for implementing the business logic of the application. Service classes are used to encapsulate the business logic of the application and provide a well-defined API for the rest of the application to use.
  • The @Service annotation is used to mark a class as a service component and make it eligible for dependency injection. This allows the class to be easily reused and tested.
  • A service class typically contains methods that perform business logic and these methods can be called by other components of the application such as controllers, repositories, and other service classes.
  • It’s also important to note that @Service classes are typically used in conjunction with other Spring annotations such as @Autowired, @Transactional, and @Cacheable to handle transactions and caching.

@Controller

  • @Controller is a Spring annotation that is used to indicate that a class is a web controller and should be managed by the Spring framework. It’s used to indicate that a class is a web controller and is responsible for handling web requests and returning appropriate web responses.
  • A controller class is typically used in the presentation layer of the application, and it’s the entry point for handling web requests. The @Controller annotation is used to mark a class as a web controller and make it eligible for handling web requests.
  • A controller class typically contains methods that handle web requests, and these methods are annotated with @RequestMapping, which is used to map a web request to a method. The @Controller class can handle different types of web requests such as GET, POST, PUT, and DELETE, etc.
  • It’s also important to note that @Controller classes are typically used in conjunction with other Spring annotations such as @Autowired, @RequestMapping, @ResponseBody, and @ExceptionHandler to handle web requests and responses.