I understand the difference between @Component and @Controller, @Component and @Repository, but was not able to find what additional feature we get in @Service as compared to @Component.
Asked
Active
Viewed 1.0k times
8
-
1Answered: https://stackoverflow.com/questions/6827752/whats-the-difference-between-component-repository-service-annotations-in – Francesc Recio May 22 '19 at 15:38
1 Answers
10
We can directly use @Component
for each and every bean, but for better understanding and maintainability of a large application, we use @Controller, @Service, @Repository
.
@Component: generic stereotype for any Spring-managed component
@Service: stereotype for service layer
@Component
Definitions of @Controller, @Service and @Repository annotations which tells that @Service is a special type of @Component. Special type annotations are also scanned because they themselves are annotated with @Component
annotation, which means they are also @Component
s. If we define our own custom annotation and annotate it with @Component
, it will also get scanned with <context:component-scan>
@Component
public @interface Service {
….
}
@Component
public @interface Repository {
….
}
@Component
public @interface Controller {
…
}
@Service
@Service
beans hold the business logic and call methods in the repository layer.

Romil Patel
- 12,879
- 7
- 47
- 76