bean的作用域

2021-09-24 01:33:47 字數 4325 閱讀 2308

什麼是bean的作用域?

bean的作用域指的就是spring容器建立bean完成後的生命週期,就是從建立到銷毀的整個過程。spring預設的作用域是singleton。

**在singleton的作用域下每乙個bean例項只會被建立一次,**而spring容器在整個生命週期中都可以使用該例項,因此在預設情況下singleton,spring容器建立bean後,通過**獲取bean,不論多少次都是乙個bean例項,可以通過標籤中的scope屬性來指定乙個bean的作用域

除了使用singleton作用域之外,還有一種常見的作用域prototype,表示每次獲取bean例項的時候都會建立乙個例項物件,類似於new操作符

@test

public void test2()

通過結果可以發現是兩個不同的例項物件

除了在xml檔案中的bean標籤中宣告bean作用域scope為prototype之外,也可以通過註解宣告

@scope("prototype")

pubic class accountdaoimpl

singleton和prototype的特殊場景

乙個作用域為singleton的作用域依賴於乙個作用域為prototype作用域的bean時候,如:

@override

public void dosomething()

@override

} private accountdao getaccountdao()

}//載入配置檔案

//測試獲取不同例項的accountdao

這種情況下,每次獲取到的acountdao都是不相同的,解決了乙個singleton例項中包含乙個prototype型別的bean的問題,另一種情況是在乙個prototype的bean中依賴於乙個singleton作用域的bean,解決的方案相同。讓singleton的bean的實現類中重寫方法。

注意:當乙個bean被設定為prototype之後spring就不會對這個bean的整個生命週期負責,容器在初始化配置或者裝飾乙個prototype例項之後,將其交給客戶端隨後就對prototype例項不管不問了,因此要慎重的使用,一般情況下,對有狀態的bean都應該使用prototype作用域,對無狀態的bean則應該使用singleton作用域。有狀態就是該bean有儲存資訊的能力,不能共享,否則會造成執行緒安全問題,而無狀態則不會儲存資訊,是執行緒安全的,可以共享,spring中大部分的bean都是singleton的,整個生命週期只有乙個。

spring中針對web程式引入了request和session兩種作用域

對於request作用域,每次http請求到達應用程式的時候,spring容器就會建立乙個新的request作用域的bean,並且該bean例項僅僅在當前http request內有效,整個請求過程中也只會使用相同的bean例項,因此可以根據需要放心的更改所建立例項的內部狀態。而其他http請求則會建立引得bean例項互不干擾,當處理請求結束,request作用域的bean例項將會被銷毀。如:在接收引數的時候需要乙個bean例項來裝載一些引數,顯然每次請求的引數都是不一樣的,因此就希望bean例項每次都是足夠的新而且只會在request作用域範圍內有效。

對於session作用域,每次建立乙個顯得http session的時候就會建立乙個session作用域的bean,並且該例項會伴隨著會話的存在而存在

@component

@scope(value = "singleton")

public class singletonbean

@component

@scope(value = "prototype" , proxymode = scopedproxymode.target_class)

public class prototypebean

@component

@scope(value = "request" , proxymode = scopedproxymode.target_class)

public class requestbean

@component

@scope(value = "session" , proxymode = scopedproxymode.target_class)

public class sessionbean

在這裡建立了4個不同作用域的bean,並使用註解的方式進行開發,@component表明他們是元件型別,需要spring容器幫忙建立@scope表明了是什麼作用域,除了sigletonbean之外其他bean還使用proxymode指明了是哪一種**模式建立的,這裡沒有介面,因此使用cglib**生成,接著需要在xml新增包掃瞄

使用springmvc建立web訪問層

@controller

public class bookcontroller

public void print()

啟動之後進行訪問,使用瀏覽器連續訪問兩次,結果為:

first  time singletonbean is :com.zejian.spring.dto.singletonbean@2ebdd720

second time singletonbean is :com.zejian.spring.dto.singletonbean@2ebdd720

first time prototypebean is :com.zejian.spring.dto.prototypebean@1ed53cde

second time prototypebean is :com.zejian.spring.dto.prototypebean@35c052be

first time requestbean is :com.zejian.spring.dto.requestbean@15b9dfe1

second time requestbean is :com.zejian.spring.dto.requestbean@15b9dfe1

first time sessionbean is :com.zejian.spring.dto.sessionbean@5b355dae

second time sessionbean is :com.zejian.spring.dto.sessionbean@5b355dae

****************************************===

first time singletonbean is :com.zejian.spring.dto.singletonbean@2ebdd720

second time singletonbean is :com.zejian.spring.dto.singletonbean@2ebdd720

first time prototypebean is :com.zejian.spring.dto.prototypebean@7775fd09

second time prototypebean is :com.zejian.spring.dto.prototypebean@79b20d97

first time requestbean is :com.zejian.spring.dto.requestbean@7d8d9679

second time requestbean is :com.zejian.spring.dto.requestbean@7d8d9679

first time sessionbean is :com.zejian.spring.dto.sessionbean@5b355dae

second time sessionbean is :com.zejian.spring.dto.sessionbean@5b355dae

****************************************===

Bean的作用域

bean元素有乙個scope屬性,用於定義bean的作用域,該屬性有如下五個值 1 singleton 單例模式,在整個spring ioc容器中,單例模式作用域的bean都將只生成乙個例項。一般spring容器預設bean的作用域為singleton 2 prototype 與singleton相...

Bean的作用域

singleton 單例 代表在spring ioc容器中只有乙個bean例項 預設的scope prototype 多例每一次從 spring 容器中獲取時,都會返回乙個新的例項 request 用在web開發中,將bean物件request.setattribute 儲存到request域中 s...

Bean的作用域

在spring中,可以在元素的scope屬性裡設定bean的作用域 預設情況下,spring只為每個在ioc容器裡宣告的bean建立唯一乙個例項,整個ioc容器範圍內都能共享該例項 所有後續的getbean 呼叫和bean引用都將返回這個唯一的bean例項。該作用域被稱為singleton,它是所有...