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

Carvia Tech | August 12, 2019 | 2 min read | 199 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

The @Repository annotation is a marker for any class that fulfills the role oor stereotype of a repository (also known as DAO - Data Access Object). One of the main uses of this marker is the automatic translation of exceptions using an implementation of PersistenceExceptionTranslator. DAO throws a subclass of a HibernateException (if we are using Hibernate), which is a RuntimeException. Spring will automatically convert these Hibernate specific Runtime Exceptions to Spring specific exception.

<!-- Exception translation bean post processor -->
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

The postprocessor automatically looks for all exception translators (implementations of the PersistenceExceptionTranslator interface) and advises all beans marked with the @Repository annotation so that the discovered translators can intercept and apply the appropriate translation on the thrown exceptions.

Some of the exception translations from HibernateExceptionTranslator are:

Exception Translation from Hibernate to Spring (HibernateExceptionTranslator)
Hibernate Exception Spring Exception

NonUniqueObjectException

DuplicateKeyException

StaleObjectStateException

ObjectOptimisticLockingFailureException

TransientObjectException

InvalidDataAccessApiUsageException

ConstraintViolationException

DataIntegrityViolationException

@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.

Summary

@Component

generic stereotype for any Spring-managed component

@Repository

stereotype for persistence layer (DAO classes)

@Service

stereotype for service layer

@Controller

stereotype for presentation layer (spring-mvc)


Buy my ebook for complete question bank

Most of these questions has been answered in my eBook "Cracking the Core Java Interview" updated on June 2018, that you can buy from this link:

Buy from Shunya (DRM Free PDF download with updates)

Top articles in this category:
  1. Running Spring Boot app as a service in unix
  2. What are different Bean Scopes in Spring?
  3. Retrofit vs FeignClient on Server Side with Spring Cloud
  4. How will you handle unresolved circular dependency in spring dependency injection?
  5. Spring DI - Singleton beans with prototype-bean dependencies
  6. Spring Boot 2.0 Reactive Web Performance Metrics
  7. Run method on application startup in Spring Boot



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 week ago


Recommended books for interview preparation:

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