Spring中bean的作用域

2021-07-29 10:43:50 字數 474 閱讀 8132

本文以spring4.1.6 為例

初期的spring中bean有兩種:單例(singleton)和 原型(prototype)

spring 預設bean的scope 是 singleton,對於有狀態的bean 不能重複使用需要設定scope 為prototype

即每次組裝時生成乙個全新的bean 的例項

**示例:

@component

@scope(configurablebeanfactory.scope_prototype)

booservie service;

xml配置bean

id=「mybean」

scope="session"/>

@component

booservie service;

Spring中Bean的作用域

bean作用域 作用域描述 singleton 在每個spring ioc容器中乙個bean定義對應乙個物件例項。整個容器中只有該bean的唯一例項 prototype 乙個bean定義對應多個物件例項。request session global session 因為這樣在配置中的任何錯誤就會即刻...

spring中bean的作用域

如何使用spring的作用域 這裡的scope就是用來配置spring bean的作用域,它標識bean的作用域。在spring2.0之前bean只有2種作用域即 singleton 單例 non singleton 也稱prototype spring2.0以後,增加了session reques...

Spring中bean的作用域

1 singleton作用域 當乙個bean的作用域設定為singleton,spring ioc容器只會建立該bean定義的唯一例項。2 prototype prototype作用域部署的bean,每一次請求都會產生乙個新的bean例項,相當與乙個new的操作。3 request request表...