What is difference between Component, Repository, Service, Controller & RestController

Carvia Tech | April 27, 2019 at 07:25 PM | 149 views


Spring provide 4 basic stereotype annotations.

@Component

@Component is a generic stereotype for any Spring-managed component. Such classes are considered as candidates for auto-detection when using annotation-based configuration and classpath scanning.

Rest three annotations (@Repository, @Service, @Controller & @RestController) are meant for a specific purpose.

@Repository

@Repository serves as a specialization of @Component Indicating that an annotated class is a "Repository", originally defined by Domain-Driven Design (Evans, 2003) as "a mechanism for encapsulating storage, retrieval, and search behavior which emulates a collection of objects". We can mark our DAO classes with @Repository.

@Service

It is a specialization of @Component Indicating that an annotated class is a "Service", originally defined by Domain-Driven Design (Evans, 2003) as "an operation offered as an interface that stands alone in the model, with no encapsulated state." May also indicate that a class is a "Business Service Facade" (in the Core J2EE patterns sense), or something similar.

@Controller

@Controller is a specialization of @Component hinting that the annotated class is a Web Controller.

@RestController

A convenience annotation that is itself annotated with @Controller and @ResponseBody. So you do not need to annotate each Controller method with @ResponseBody if you have @RestController at the class level.

Summary

@Component

generic stereotype for any Spring-managed component

@Repository

stereotype for persistence layer

@Service

stereotype for service layer

@Controller

stereotype for presentation layer (spring-mvc)

@RestController

stereotype for presentation REST layer (spring-mvc)

That’s all!


Top articles in this category:
  1. Running Spring Boot app as a service in unix
  2. What are different Bean Scopes in Spring?
  3. How will you handle unresolved circular dependency in spring dependency injection?
  4. Spring DI - Singleton beans with prototype-bean dependencies
  5. Run method on application startup in Spring Boot
  6. Spring Boot 2.0 Reactive Web Performance Metrics
  7. What is N+1 problem in Hibernate, how will you identify and solve it?


Find more on this topic:
Spring Framework image
Spring Framework

Spring Framework - MVC, Dependency Injection, Spring Hibernate, Spring Data JPA, Spring Boot and Spring Cloud for Microservices Architecture.

Last updated 1 month ago


Recommended books for interview preparation:
You may also be interested in..
Generic placeholder image
ebook PDF - Cracking Spring Microservices Interviews for Java Developers
You may also be interested in..
Generic placeholder image
ebook PDF - Cracking Java Interviews v3.5 by Munish Chandel
You may also be interested in..
Generic placeholder image
ebook PDF - Cracking Spring Microservices Interviews for Java Developers
You may also be interested in..
Generic placeholder image
ebook PDF - Cracking Java Interviews v3.5 by Munish Chandel
Facebook Page
Free Email Updates

Enter your email address to subscribe to this blog and receive notifications of new posts by email.


This website uses cookies to ensure you get the best experience on our website. more info