[Turn]what ' s The difference between @Component, @Repository & @Service annotations in Spring

Source: Internet
Author: User
Tags aop

Original address: https://www.cnblogs.com/softidea/p/6070314.html

@Component is equivalent to

<bean>

@Service, @Controller, @Repository = {@Component + some more special functionality}

That mean Service,controller and Repository is functionally the same.

The three annotations is 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 is the DAOs (Data access Objects), they access the database directly.

Now, ask why separate them: (I assume you know aop-aspect oriented programming)

Lets say you want to Monitors the Activity of the DAO Layer is only. You'll write a Aspect (a class) class that does some logging before and after every method of your DAO are invoked, you A Re able to do the using AOP as you have three distinct Layers and is not mixed.

So you can do logging of DAO "Around", "before" or "after" the DAO methods. You could do this because you had a DAO in the first place. What are just achieved is separation of concerns or tasks.

Imagine If there were only one annotation @Controller, then this component would has dispatching, business logic and acces Sing database all mixed, so dirty code!

Above mentioned is one very common scenario, there was many more use cases of what to use three annotations.

In Spring @Component , @Service , and @Controller . Am @Component stereotype annotations which is used for:

@Controller:where your request mapping from presentation page do i.e. presentation layer won ' t go to all other Fil E It goes directly to @Controller class and check for requested path in @RequestMapping annotation which written before method calls if NE Cessary.

@Service: All business logic are here i.e Data related calculations and all. This annotation of business layer in which we user not directly call persistence method so it would call the this methods Usin G this annotation. It would request @Repository as per user request

@Repository: This is a persistence layer (data Access layer) of application which used to get Data from database. i.e. all the Database related operations is done by repository.

@Component-Annotate your other components, for example REST resource classes, with component stereotype.

From Spring Documentation:

In Spring 2.0 and later, the @Repository annotation are a marker for any class that fulfills the role or stereotype (also K Nown as Data Access Object or DAO) of a repository. Among the uses of this marker is the automatic translation of exceptions.

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 is specializations of @Component for more specific use cases, for example, in the Persistence, service, and presentation layers, respectively.

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

Thus, if you be choosing between using @Component or @Service for your Service layer, @Service is clearly the better Choi Ce. Similarly, as stated above, @Repository is already supported as a marker for automatic exception translation in your Persi Stence 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)      |

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 so you can annotate your component classes with @Component, but by annotating them with @Repository, @Service, or @Controller instead, your classes is more properly suited for processing by tools or associating with aspect S. For example, these stereotype annotations make ideal targets for pointcuts. Of course, it is also possible this @Repository, @Service, and @Controller may carry additional semantics in the future Releas Es of the Spring Framework. Thus, if you were making a decision between using @Component or @Service for your Service layer, @Service was clearly the be Tter 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:-http://static.springsource.org/spring/docs/3.0.0.M3/reference/html/ch04s12.html

In general @Service, @Controller, @Repository = {@Component + special features}, the article mentions that we should combine spring-to-plane programming thinking (AOP), When our controller takes on the task of distributing the request, it also processes the business logic and interacts with the database persistence layer, so the code is bad, so the document argues for using these annotation classes to better differentiate the respective functions

    • @Component: The component will be automatically scanned
    • @Repository: Indicates the component of the DAO layer in the persistence layer (its benefit is catching the exception that occurs in the persistence layer interaction and expressing the exception in a friendly way, without this annotation, the exception that the database throws is often difficult to understand)
    • @Service: Business logic
    • @Controller: Distribution Request

[Turn]what ' s The difference between @Component, @Repository & @Service annotations in Spring

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.