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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With