Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@Service vs @Component in Spring [duplicate]

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.

like image 687
Ankit Singodia Avatar asked May 22 '19 15:05

Ankit Singodia


1 Answers

enter image description here

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 @Components. 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.

like image 74
Romil Patel Avatar answered Oct 15 '22 21:10

Romil Patel