8

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.

Romil Patel
  • 12,879
  • 7
  • 47
  • 76
Ankit Singodia
  • 581
  • 1
  • 6
  • 16
  • 1
    Answered: https://stackoverflow.com/questions/6827752/whats-the-difference-between-component-repository-service-annotations-in – Francesc Recio May 22 '19 at 15:38

1 Answers1

10

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.

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