Różnica między @Component, @Repository & @Service annotations in Spring?

0

Witam,
Jaka jest różnica między @Component, @Repository & @Service annotations in Spring?

Spring provides 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. 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. It is also possible that @Repository, @Service, and @Controller may carry additional semantics in future releases of the Spring Framework. 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.

Hmm, czyli na upartego każdą z tych trzech można zastąpić @Componenet? Jakie dodatkowe informację są przechowywane w tych adnotacjach?

0

Kurde. Zazwyczaj nie lubię jak ktoś mi tak odpowiada na forum, ale się inaczej nie da. Tak pięknie wytłumaczone na stacku. Chyba pierwszy wynik w googlu.

http://stackoverflow.com/questions/6827752/whats-the-difference-between-component-repository-service-annotations-in

Odwrotnie. Każdą adnotację możesz zastąpić @Component

2

W bazowym przypadku są to tylko markery, które mają opisywać z jakiego rodzaju komponentem mamy do czynienia i gdzie w naszej architekturze będzie siedzieć. W praktyce, tak jak masz w dokumentacji, na przykład @Repository jest tylko prostym rozszerzeniem @Component (kod: https://github.com/spring-projects/spring-framework/blob/master/spring-context/src/main/java/org/springframework/stereotype/Repository.java).
Jednak jeżeli używasz mechanizmów programowania aspektowego możesz nałożyć aspekt na komponenty oznaczone jako @Repository i tylko na te komponenty. Jeżeli wykorzystasz tylko @Component to nałożysz go na wszystkie komponenty.

Mówiąc prościej te dodatkowe adnotacje zazwyczaj pełną funkcję informacyjną, ale w pewnych przypadkach pozwalają na zawężenie pewnych mechanizmów :)

1 użytkowników online, w tym zalogowanych: 0, gości: 1