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

Upasana | August 12, 2019 | 2 min read | 731 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. Retrofit vs Feign for Server Side
  2. Unresolved circular dependency in spring dependency injection
  3. What are different Bean Scopes in Spring?
  4. Spring Boot 2.0 Reactive Web Performance Metrics
  5. Running Spring Boot app as a service in unix
  6. Feign Client Logging and connection timeout
  7. Spring DI - Singleton beans with prototype-bean dependencies

Recommended books for interview preparation:

Find more on this topic:
Buy interview books

Java & Microservices interview refresher for experienced developers.