What is the difference between @Component, @Repository, @Service, and @Controller annotations in Spring?

@Repository, @Service, and @Controller are extended from @Component. Which means technically all are the same. But there are some differences in usages and functionalities. In this article, you will an understanding of @Component, @Repository, @Service, @Controller annotations

What is the similarity of these?

All of them allow Spring to detect them as Spring-managed Beans. It is called as component-scanning. We can define what is the package that contains the beans and Spring scan all the classes annotated with @Component.

@Component is a general-purpose stereotype annotation that indicates that it is a Spring-managed bean. Since @Service, @Repository, and @Controller are also annotated with @Component, those are also scanned.

package org.springframework.stereotype;
...
@Component
public @interface Service {
   ...
}
package org.springframework.stereotype;
...
@Component
public @interface Repository {
  ...
}
package org.springframework.stereotype;
...
@Component
public @interface Controller {
   ...
}

What are the specialties of @Service, @Repository and @Controller?

Generally these annotation represents the layers of the application.

@Service – is for service layer(Business logic)

@Repository – is for data access layer(Persistent layer)

@Controller – is for presentation layer(API layer)

difference between @Component, @Repository, @Service, and @Controller annotations - Layers in the application

What are the specialties of @Controller?

@Controller annotation indicates that it is in the Presentation/API layer and serves as a controller. These classes receive requests from clients. First, the request receives by the Dispatcher servlet and it routes to the particular Controller by using the value of @RequestMapping annotation. You can only use @RequestMapping annotation on @Controller annotated classes.

What is @RestController annotation?

@RestController is again a specialized version of @Controller and it adds @Controller and @ResponseBody annotation automatically.

package org.springframework.web.bind.annotation;
...
@Controller
@ResponseBody
public @interface RestController {
...
}

This annotation is very useful when creating RESTful web services using Spring MVC. It converts responses to JSON or XML.

What are the specialties of @Repository?

@Repository annotation indicates that the class is in DAO/Persistent/Data access layer. The main specialty on @Respository annotation over other annotations is it supports automatic exception translation. Exception translation means converting low-level exceptions into high-level Spring exceptions.

What are the specialties of @Service?

Apart from scanning and representing the layer which is the business logic happens, at the moment it does not provide any other functionality. Of cause you can call to database with a bean annotated with @Service annotation but better to strict to the convention as Spring may introduce some feature in future releases.

Conclusion

AnnotationUsages
@ComponentGeneric annotation for any Spring-managed bean
@RepositoryFor DAO/Data access layer. Support exception translation
@ServiceFor Service/Busness logic layer
@ControllerFor Presentation layer. Support @RequestMapping annotation
which allows to map client requests to a specific method
@RestControllerAllows to create RESTful web services. Translate responses to
JSON/XML formats.
Difference between @Component, @Repository, @Service, and @Controller annotations in Spring

Related articles

Understanding @ControllerAdvice annotation

Understanding Spring AOP

18 thoughts on “What is the difference between @Component, @Repository, @Service, and @Controller annotations in Spring?

  1. Having read this I thought it was really enlightening. I appreciate you spending some time and effort to put this article together. I once again find myself spending a significant amount of time both reading and leaving comments. But so what, it was still worthwhile! Susie Yul Carmine

  2. Do you mind if I quote a couple of your posts as long as I provide credit
    and sources back to your site? My website is in the very same area of interest as yours
    and my visitors would genuinely benefit from some
    of the information you provide here. Please let me know if this alright with you.
    Thanks a lot!

  3. My relatives every time say that I am wasting my time here at net, however I
    know I am getting know-how all the time by reading thes
    good articles.

  4. fantastic points altogether, you simply received a new reader. What may you suggest in regards to your publish that you just made a few days ago? Any certain?

Leave a Reply

Your email address will not be published. Required fields are marked *