1. 程式人生 > >Spring 之 Bean 的 Scope

Spring 之 Bean 的 Scope

Bean 的 Scope

Scope 描述的是 Spring 容器如何新建 Bean  的例項的. Spring 的 Scope 有以下幾種,通過 @Scope 註解來實現.

  1. Singleton: 一個 Spring 容器中只有一個 Bena 的例項,此為 Spring 的預設配置,全容器共享一個例項.
  2. Prototype:  每次使用新建一個 Bean 的例項.
  3. Request: Web 專案中,給每一個 HTTP request 新建一個 Bean 例項.
  4. Session : Web 專案中,給每一個 HTTP session 新建一個 Bean 例項.
  5. GlobalSession:  這個只在 portal 應用中有用,給每一個 global HTTP session 新建一個 Bean  例項.
  6. StepScope:  只有在 Spring Batch  中使用.