淺談Spring Bean的作用域之間有什么區(qū)別
前言
Spring的 bean有5種作用域分別是:singleton、prototype、request、session和globalSession

Spring Bean的作用域之間有什么區(qū)別?
? 在Spring中,可以在<bean>元素的scope屬性里設(shè)置bean的作用域,以決定這個bean是單例的還是多例的。
? 默認(rèn)情況下,Spring只為每個在IOC容器里聲明的bean創(chuàng)建唯一一個實例,整個IOC容器范圍內(nèi)都能共享該實例:所有后續(xù)的getBean()調(diào)用和bean引用都將返回這個唯一的bean實例。該作用域稱為singleton,它是bean的默認(rèn)作用域。
作用域的類別跟說明
- singleton:在SpringIOC容器中僅存在一個Bean實例,Bean以單實例的方式存在
- prototype:每次調(diào)用getBean()時都會返回一個新的實例
- request:每次HTTP請求都會創(chuàng)建一個新的Bean,該作用域僅適用于WebApplicationContext環(huán)境
- session:同一個HTTP Session共享一個Bean,不同的HTTP Session使用不同的Bean,該作用域僅適用于WebApplicationContext環(huán)境
簡述
bean的作用域:可以通過元素的scope屬性來指定bean作用域
- singleton:默認(rèn)值。當(dāng)IOC容器一創(chuàng)建就會創(chuàng)建bean的實例,而且是單例的,每次得到的都是同一個
- prototype:原型的。當(dāng)IOC容器一創(chuàng)建不再實例化該bean,每次調(diào)用getBean方法時再實例化該bean,而且每調(diào)用
- request:每次請求實例化一個bean
- session:在一次會話中共享一個bean
測試用例
因為平時使用SPRING MVC開發(fā)的時候比較多,有必要了解清楚怎么去調(diào)用這幾種作用域。
1. 定義不同作用域的java類
@Component
@Scope( "session")
public class SessionObj {
}
@Component
@Scope( "request")
public class RequestObj {
}
@Component
@Scope( "prototype")
public class PrototypeObj {
}
@Component
@Scope( "singleton")
public class SingletonObj {
}
2. 注入到controller,由于controller是單例的,因此必須通過實現(xiàn)ApplicationContextAware接口,直接從容器中取出對象。
因此測試的controller是:
@Controller
public class IndexController implements ApplicationContextAware {
private RequestObj RequestObj;
private SessionObj SessionObj;
private PrototypeObj PrototypeObj;
private SingletonObj SingletonObj;
private ApplicationContext applicationContext;
@RequestMapping("/")
@ResponseBody
public String index() {
print();
return "Welcome";
}
public void print() {
System.out.println("first time singleton is :" + getSingletonObj());
System.out.println("second time singleton is :" + getSingletonObj());
System.out.println("first time prototype is :" + getPrototypeObj());
System.out.println("second time prototype is :" + getPrototypeObj());
System.out.println("first time request is :" + getRequestObj());
System.out.println("second time request is :" + getRequestObj());
System.out.println("first time session is :" + getSessionObj());
System.out.println("second time session is :" + getSessionObj());
}
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.applicationContext = applicationContext;
}
public RequestObj getRequestObj() {
return applicationContext.getBean(RequestObj.class);
}
public void setRequestObj(RequestObj requestObj) {
RequestObj = requestObj;
}
public SessionObj getSessionObj() {
return applicationContext.getBean(SessionObj.class);
}
public void setSessionObj(SessionObj sessionObj) {
SessionObj = sessionObj;
}
public PrototypeObj getPrototypeObj() {
return applicationContext.getBean(PrototypeObj.class);
}
public void setPrototypeObj(PrototypeObj prototypeObj) {
PrototypeObj = prototypeObj;
}
public SingletonObj getSingletonObj() {
return applicationContext.getBean(SingletonObj.class);
}
public void setSingletonObj(SingletonObj singletonObj) {
SingletonObj = singletonObj;
}
}
3. 運行結(jié)果
//使用chrome第一次打印數(shù)據(jù): first time singleton is :com.fb.po.SingletonObj@1e3223e second time singleton is :com.fb.po.SingletonObj@1e3223e first time prototype is :com.fb.po.PrototypeObj@3e683f second time prototype is :com.fb.po.PrototypeObj@12e18d7 first time request is :com.fb.po.RequestObj@1d45706 second time request is :com.fb.po.RequestObj@1d45706 first time session is :com.fb.po.SessionObj@9a6b2e second time session is :com.fb.po.SessionObj@9a6b2e //使用chrome打印第二次數(shù)據(jù) first time singleton is :com.fb.po.SingletonObj@1e3223e second time singleton is :com.fb.po.SingletonObj@1e3223e first time prototype is :com.fb.po.PrototypeObj@122e5be second time prototype is :com.fb.po.PrototypeObj@192add first time request is :com.fb.po.RequestObj@4d1b6c second time request is :com.fb.po.RequestObj@4d1b6c first time session is :com.fb.po.SessionObj@9a6b2e second time session is :com.fb.po.SessionObj@9a6b2e //使用IE打印第三次數(shù)據(jù) first time singleton is :com.fb.po.SingletonObj@1e3223e second time singleton is :com.fb.po.SingletonObj@1e3223e first time prototype is :com.fb.po.PrototypeObj@10f1ecb second time prototype is :com.fb.po.PrototypeObj@1aeb990 first time request is :com.fb.po.RequestObj@18a1e7 second time request is :com.fb.po.RequestObj@18a1e7 first time session is :com.fb.po.SessionObj@12d5c55 second time session is :com.fb.po.SessionObj@12d5c55
4.結(jié)果分析
從結(jié)果來看,單例的bean的三次的數(shù)據(jù)都是打印一樣的(默認(rèn)的bean的級別就是單例);
prototype的bean每次的數(shù)據(jù)都是不一樣的,每次請求的時候調(diào)用兩次結(jié)果都不一樣。
request的bean在每次request的時候都不一致,但是同一次request返回的數(shù)據(jù)是一致的。
session的bean在前兩次結(jié)果一致,最后一次數(shù)據(jù)不一致,和session的節(jié)奏是一致的。
5. 欠缺
網(wǎng)絡(luò)上說必需配置
<listener> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener>
但是沒配置也好使……好奇……
最后一種作用域是適用于portlet,沒試驗,據(jù)說是在多個session之間可以共享,效果等同于全局變量。
到此這篇關(guān)于淺談Spring Bean的作用域之間有什么區(qū)別的文章就介紹到這了,更多相關(guān)Spring Bean作用域內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JDK9為何要將String的底層實現(xiàn)由char[]改成了byte[]
String 類的源碼已經(jīng)由?char[]?優(yōu)化為了?byte[]?來存儲字符串內(nèi)容,為什么要這樣做呢?本文就詳細(xì)的介紹一下,感興趣的可以了解一下2022-03-03
springboot中添加靜態(tài)頁面的三種實現(xiàn)方案與對比
這篇文章主要為大家詳細(xì)介紹了springboot中添加靜態(tài)頁面的三種實現(xiàn)方案與對比,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-11-11
淺談Java中SimpleDateFormat 多線程不安全原因
SimpleDateFormat是Java中用于日期時間格式化的一個類,本文主要介紹了淺談Java中SimpleDateFormat 多線程不安全原因,感興趣的可以了解一下2024-01-01
SpringBoot整合MybatisPlus實現(xiàn)增刪改查功能
MybatisPlus是國產(chǎn)的第三方插件,?它封裝了許多常用的CURDapi,免去了我們寫mapper.xml的重復(fù)勞動。本文將整合MybatisPlus實現(xiàn)增刪改查功能,感興趣的可以了解一下2022-05-05
SpringBoot之spring.factories的使用方式
這篇文章主要介紹了SpringBoot之spring.factories的使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01
解析SpringBoot @EnableAutoConfiguration的使用
這篇文章主要介紹了解析SpringBoot @EnableAutoConfiguration的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09

