Spring註解 @Component @Service, @Controller, @Repository

以前留下整理的資料的記錄。

spring註解

@Component, @Service, @Controller, @Repository是spring註解,註解後可以被spring框架所掃描並註入到spring容器來進行管理

  • @Component是通用註解,其他三個註解是這個註解的拓展,並且具有了特定的功能(generic stereotype for any Spring-managed component)。
  • @Repository註解在持久層中,具有將數據庫操作拋出的原生異常翻譯轉化為spring的持久層異常的功能。(stereotype for persistence layer)
  • @Controller層是spring-mvc的註解,具有將請求進行轉發,重定向的功能。(stereotype for presentation layer)
  • @Service層是業務邏輯層註解,這個註解只是標註該類處於業務邏輯層。 (stereotype for service layer)
    用這些註解對應用進行分層之後,就能將請求處理,義務邏輯處理,數據庫操作處理分離出來,為代碼解耦,也方便了以後項目的維護和開發。

註解:
Spring2.0時提供的: @Repository(在Spring2.5有修改部分內容)
Spring2.5時提供的: @Component, @Controller, @Service

more info:stackoverflow 參考
more info:@Repository doc
more info:@Component doc
more info:@Controller doc
more info:@Service doc