What’s the difference between @Component, @Controller, @Repository & @Service annotations in Spring? - CodeByAkram

What’s the difference between @Component, @Controller, @Repository & @Service annotations in Spring?


Spring Annotations, Codebyakram


Spring 2.5 introduces further stereotype annotations: @Component@Service, and @Controller@Component is a generic stereotype for any Spring-managed component. @Repository@Service, and @Controller are specializations of @Component for more specific use cases, for example, in the persistence, service, and presentation layers, respectively.

You may also like What do you mean by Aspect, Join Point, Advice?

Therefore, you can annotate your component classes with @Component, but by annotating them with @Repository@Service, or @Controller instead, your classes are more properly suited for processing by tools or associating with aspects. For example, these stereotype annotations make ideal targets for pointcuts.

Thus, if you are choosing between using @Component or @Service for your service layer, @Service is clearly the better choice. Similarly, as stated above, @Repository is already supported as a marker for automatic exception translation in your persistence layer.



No comments:

Post a Comment