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

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
Annotation | Usages |
@Component | Generic annotation for any Spring-managed bean |
@Repository | For DAO/Data access layer. Support exception translation |
@Service | For Service/Busness logic layer |
@Controller | For Presentation layer. Support @RequestMapping annotation which allows to map client requests to a specific method |
@RestController | Allows to create RESTful web services. Translate responses to JSON/XML formats. |
Learned few new things. Thank you.
Happy to hear that. Thank you.
Simply explain everything. Well structured article.
Thank you very much.
Very informative and to the point
Thank you very much.
I love it whenever people come together and share views. Great site, keep it up! Kaia Pietro Melcher
I quite like reading a post that can make people think. Also, thanks for allowing for me to comment!| Claribel Jerome Simone
Hey! Would you mind if I share your blog with my facebook group? Charis Lee Aili
I do not even know the way I ended up right here, but I believed this post used to be good. Sheryl Jarrett Liddle
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
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!
I am grateful to you for this beautiful content. I have included the content in my favorites list and will always wait for your new blog posts.
Nice articles! Favorite add and blog love.
Awesome article.Really looking forward to read more. Cool.
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.
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?
Oha ?
very nice post, i definitely love this website, keep on it