1. 程式人生 > >[轉]what’s the difference between @Component ,@Repository & @Service annotations in Spring

[轉]what’s the difference between @Component ,@Repository & @Service annotations in Spring

fir 結合 invoke doc repo supported rest 持久 can

原文地址: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 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.

In Spring @Component, @Service, and @Controller. @Component are Stereotype annotations which is used for:

@Controller: where your request mapping from presentation page done i.e. Presentation layer won‘t go to any other file it goes directly to @Controller class and check for requested path in @RequestMapping annotation which written before method calls if necessary.

@Service: All business logic is here i.e. Data related calculations and all.This annotation of business layer in which our user not directly call persistence method so it will call this methods using this annotation. It will request @Repository as per user request

@Repository:This is Persistence layer(Data Access Layer) of application which used to get data from database. i.e. all the Database related operations are 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 is a marker for any class that fulfills the role or stereotype (also known 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 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.

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.

| 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 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 :- http://static.springsource.org/spring/docs/3.0.0.M3/reference/html/ch04s12.html


總的來說就是 @Service, @Controller , @Repository = {@Component + 特殊的功能} ,文章提到我們應該結合Spring中到的切面編程思想(AOP), 我們的controller 承擔著分發請求的任務後,又要處理業務邏輯,同時還要與數據庫持久層作交互,這樣的代碼是糟糕的,所以文檔中主張是使用這幾個註解類,更好地區分開各自的功能

  • @Component : 將自動掃描組件
  • @Repository : 指示為在持久層的Dao層的組件(它的好處是捕抓到持久層交互中出現的異常,並把異常友好地表示出來,假如沒有這個註釋,數據庫拋出的異常常常難以理解)
  • @Service : 業務邏輯
  • @Controller : 分發請求

[轉]what’s the difference between @Component ,@Repository & @Service annotations in Spring