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

后端 未结 29 2136
时光说笑
时光说笑 2020-11-22 00:33

Can @Component, @Repository and @Service annotations be used interchangeably in Spring or do they provide any particular functionality besides acting as a notation device?

相关标签:
29条回答
  • 2020-11-22 01:04

    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 within the defined layer.

    @Component: It is a basic auto component scan annotation, it indicates annotated class is an auto scan component.

    @Controller: Annotated class indicates that it is a controller component, and mainly used at the presentation layer.

    @Service: It indicates annotated class is a Service component in the business layer.

    @Repository: You need to use this annotation within the persistence layer, this acts like database repository.

    One should choose a more specialised form of @Component while annotating their class as this annotation may contain specific behavior going forward.

    0 讨论(0)
  • 2020-11-22 01:06

    From Spring Documentation:

    The @Repository annotation is a marker for any class that fulfils the role or stereotype of a repository (also known as Data Access Object or DAO). Among the uses of this marker is the automatic translation of exceptions, as described in Exception Translation.

    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 (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. @Repository, @Service, and @Controller can also 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 earlier, @Repository is already supported as a marker for automatic exception translation in your persistence layer.

    ┌──────────────┬─────────────────────────────────────────────────────┐
    │ 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)      │
    └──────────────┴─────────────────────────────────────────────────────┘
    
    0 讨论(0)
  • 2020-11-22 01:06

    Spring 2.5 introduces further stereotype annotations: @Component, @Service and @Controller. @Component serves as a generic stereotype for any Spring-managed component; whereas, @Repository, @Service, and @Controller serve as specializations of @Component for more specific use cases (e.g., in the persistence, service, and presentation layers, respectively). What this means is that 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. Of course, it is also possible that @Repository, @Service, and @Controller may carry additional semantics in future releases of the Spring Framework. Thus, if you are making a decision 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.

    @Component – Indicates a auto scan component.
    @Repository – Indicates DAO component in the persistence layer.
    @Service – Indicates a Service component in the business layer.
    @Controller – Indicates a controller component in the presentation layer.
    

    reference :- Spring Documentation - Classpath scanning, managed components and writing configurations using Java

    0 讨论(0)
  • 2020-11-22 01:06

    @Component: you annotate a class @Component, it tells hibernate that it is a Bean.

    @Repository: you annotate a class @Repository, it tells hibernate it is a DAO class and treat it as DAO class. Means it makes the unchecked exceptions (thrown from DAO methods) eligible for translation into Spring DataAccessException.

    @Service: This tells hibernate it is a Service class where you will have @Transactional etc Service layer annotations so hibernate treats it as a Service component.

    Plus @Service is advance of @Component. Assume the bean class name is CustomerService, since you did not choose XML bean configuration way so you annotated the bean with @Component to indicate it as a Bean. So while getting the bean object CustomerService cust = (CustomerService)context.getBean("customerService"); By default, Spring will lower case the first character of the component – from ‘CustomerService’ to ‘customerService’. And you can retrieve this component with name ‘customerService’. But if you use @Service annotation for the bean class you can provide a specific bean name by

    @Service("AAA")
    public class CustomerService{
    

    and you can get the bean object by

    CustomerService cust = (CustomerService)context.getBean("AAA");
    
    0 讨论(0)
  • 2020-11-22 01:07

    In Spring 4, latest version:

    The @Repository annotation is a marker for any class that fulfills the role or stereotype of a repository (also known as Data Access Object or DAO). Among the uses of this marker is the automatic translation of exceptions as described in Section 20.2.2, “Exception translation”.

    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.

    0 讨论(0)
提交回复
热议问题