1. 程式人生 > >Spring Configuration(二):Bean的Scope

Spring Configuration(二):Bean的Scope

Scope描述的是Spring容器如何新建Bean的例項,通過@Scope註解來實現:

    Spring容器最初提供了兩種bean的scope型別:singleton和prototype,但釋出2.0以後,又引入了另外三種scope型別:request、session和global session,這三種只能在web 應用中才可以使用。

    ①singleton:一個Spring容器只有一個Bean例項,此為Spring的預設配置。@Scope("Singleton")
    ②prototype:每次呼叫新建一個Bean的例項
    ③request:Web專案中,給每個http Request新建一個Bean例項
    ④session:Web專案中,給每個http session新建一個Bean例項
    ⑤globalSession:這個旨在portal應用中有用,給每個global http session新建一個Bean例項
    另外在Spring Batch中還有一個Scope是使用@StepScope,在批處理中使用。