2014年1月7日 星期二

[Spring] @Component, @Scope

  • 標注了 @Component,@Service,@Controller,@Repository 的 class,定義為 bean 也就由 Spring 管理 class lifecycle (creation/destruction)。
  • 作用和在 XML 文件中使用 <bean> 一樣。


@Service
  • 表示是邏輯層物件。
@Controller
  • 表示是控制層物件, 如 struts 中的 action。
@Repository
  • DAO 物件。
@Component
  • 泛指沒有明確歸類的元件。


@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.


@Scope
  • Spring container 要回傳哪個 bean instance。
  • singleton(default), prototype, request, session, globalSession。
    • singleton: container 中只存在一個 instance。
    • prototype: 每個取得都是新的 instance



* Reference
- 4.4 Bean scopes
- Spring Bean Scopes Example
- What's the difference between @Component, @Repository & @Service annotations in Spring?

沒有留言:

張貼留言