Spring provides four different types of auto component scan annotations, they are @Component, @Service, @Repository and @Controller. Technically, there is no difference between them, but every auto component scan annotation should be used for a special purpose and with in the defined layer. Here is the purpose of each annotation
@Component: It is a basic auto component scan annotation, it indicates annotated class is a auto scan component.
@Repository: You need to use this annotation with in the persistance layer, which acts like database repository.
@Service: It indicates annotated class is a Service component in the business layer.
@Controller: Annotated class indicates that it is a controller components, and mainly used at presentation layer.
@Repository: You need to use this annotation with in the persistance layer, which acts like database repository.
@Service: It indicates annotated class is a Service component in the business layer.
@Controller: Annotated class indicates that it is a controller components, and mainly used at presentation layer.
As above said, technically there is no difference between them, all of the 3 annotations @Service, @Repository and @Controller implementations are annotated by @Controller annotation. So technically you can use @Controller for any auto component scan, but for better readability and better standards use respective annotations.
@Component is equivalent to
<bean>
@Service, @Controller , @Repository = {@Component + some more special functionality}
That mean Service,Controller and Repository are functionally the same.
The three annotations are used to separate "Layers" in your application,
- Controllers just do stuff like dispatching, forwarding, calling service methods etc.
- Service Hold business Logic, Calculations etc.
- Repository are the DAOs(Data Access Objects), they access the database directly.
Now you may ask why separate them:(I assume you know AOP-Aspect Oriented Programming)
Lets say you want to Monitors the Activity of the DAO Layer only. You will write an Aspect(A class) class that does some logging before and after every method of your DAO is invoked, you are able to do that using AOP as you have three distinct Layers and are not mixed.
So you can do logging of DAO "around", "before" or "after" the DAO methods. You could do that because you had a DAO in the first place. What you just achieved is Separation of concerns or tasks.
Imagine if there were only one annotation @Controller, then this component will have dispatching, business logic and accessing database all mixed, so dirty code!
Above mentioned is one very common scenario, there are many more use cases of why to use three annotations.
| Annotation | Meaning |
+------------+-----------------------------------------------------+
| @Component | generic stereotype for any Spring-managed component |
| @Repository| stereotype for persistence layer |
| @Service | stereotype for service layer |
| @Controller| stereotype for presentation layer (spring-mvc) |
