Spring @Component,@ Service,@ Repository,@ Controller差異
Spring @Component,@ Service,@ Repository和@Controller注釋用于在Spring架構中掃描指定路徑的類注冊為bean。
@Component是一個通用注釋。
@ Service,@ Repository,@ Controller與@Component的差別在于它們是@Component的特例,用于特定目的。差別僅在于分類。
對于所有這些注釋(刻闆印象),從技術上講,核心目的是相同的。
Spring自動掃描并識别所有使用“ @ Component,@ Service,@ Repository,@ Controller ” 注釋的類,并可以使用ApplicationContext擷取這些bean。
對于所有@Component,@ Service,@ Repository和@Controller原型元件,都是根據BeanNameGenerator政策配置設定bean名稱。即類名首字母小寫 。
我們還可以在注釋期間提供我們的名稱選擇,這将是高優先級
他們的差別就好像讓你在下圖中找不同點

我是找不出來的。如果誰知道他們的不同請部落格下留言
2018 8 15 再次編輯 時隔大半年了,
下面的話是春天告訴我的
在Spring 2.0及更高版本中,
@Repository
注釋是任何滿足存儲庫的角色或構造型(也稱為資料通路對象或DAO)的類的标記。該标記的用途之一是異常的自動轉換。
Spring 2.5中引入了進一步典型化注解:
,
@Component
,和
@Service
。
@Controller
是任何Spring管理元件的通用構造型。
@Component
,
@Repository
和,并且
@Service
是
@Controller
@Component
更具體的用例的特化,例如,分别在持久性,服務和表示層中。
是以,你可以用你的注解元件類
,但如果用注解它們
@Component
,
@Repository
或者
@Service
@Controller
,你的類能更好地被工具處理,或與切面進行關聯。例如,這些刻闆印象注釋成為切入點的理想目标。
是以,如果您在使用
或
@Component
服務層之間進行選擇,
@Service
顯然是更好的選擇。同樣,如上所述,
@Service
已經支援将其作為持久層中自動異常轉換的标記。
@Repository
┌────────────┬─────────────────────────────────────────────────────┐
│ 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) │
└────────────┴─────────────────────────────────────────────────────┘
英文好的看這個
https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#beans-stereotype-annotations
再次編輯下
@ Component,@ Repository,@ Controller和@Service之間的差異
@Component
這是一個通用的構造型注釋,表明該類是一個spring元件。
@Component的特殊之處
<context:component-scan>
僅在于掃描
@Component
并且不會查找
@Controller
,
@Service
并且
@Repository
。
掃描它們是因為它們本身都帶有注釋
@Component
。
隻要看一看
@Controller
,
@Service
和
@Repository
注釋的定義:
@Component
public @interface Service {
….
}
@Component
public @interface Repository {
….
}
@Component
public @interface Controller {
…
}
是以,說這是沒有錯的
@Controller
,
@Service
并且
@Repository
是特殊類型的
@Component
注釋。
<context:component-scan>
選擇它們并将它們的子類注冊為bean,就像它們被注釋
@Component
。
掃描它們是因為它們本身帶有
@Component
注釋注釋。如果我們定義自己的自定義注釋并使用它進行注釋
@Component
,那麼它也将被掃描
<context:component-scan>
@Repository
這是為了表明該類定義了一個資料存儲庫。
@Repository有什麼特别之處?
除了指出這是一個基于注釋的配置之外,
@Repository
我們的工作是捕獲特定于平台的異常并将它們重新抛出為Spring的統一未經檢查的異常之一。為此,我們提供了
PersistenceExceptionTranslationPostProcessor
,我們需要在Spring的應用程式上下文中添加如下:
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
這個bean後處理器為任何帶有注釋的bean添加一個顧問程式,
@Repository
以便捕獲任何特定于平台的異常,然後将其作為Spring未經檢查的資料通路異常之一重新抛出。
@Controller
該
@Controller
注解表明特定類供應控制器的作用。該
@Controller
注釋充當注解類刻闆印象,這表明它的作用。
@Controller有什麼特别之處?
我們不能與任何其他關掉這個注解像
@Service
或者
@Repository
,即使他們看起來一樣。排程程式掃描帶注釋的類
@Controller
并檢測其中的
@RequestMapping
注釋。我們隻能在帶
@Controller
注釋的類上使用
@RequestMapping
。
@服務
@Services
在存儲庫層中儲存業務邏輯和調用方法。
@Service有什麼特别之處?
除了它用于表明它持有業務邏輯這一事實之外,這個注釋沒有明顯的特點,但是誰知道,spring可能在未來增加一些額外的特殊功能。
還有什麼?
與上述類似,在未來的春天可以選擇添加特殊功能的
@Service
,
@Controller
并
@Repository
根據他們的分層約定。
是以,尊重慣例并将其與層一緻使用始終是一個好主意。