淺談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)文章
Resty開發(fā)restful版本的Jfinal深入研究
這篇文章主要為大家介紹了Resty開發(fā)restful版本的Jfinal深入研究有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-03-03maven打包web項目時同時打包為war和jar文件的方法
本篇文章主要介紹了maven打包web項目時同時打包為war和jar文件的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10圖解Java經(jīng)典算法冒泡選擇插入希爾排序的原理與實現(xiàn)
冒泡排序是一種簡單的排序算法,它也是一種穩(wěn)定排序算法。其實現(xiàn)原理是重復(fù)掃描待排序序列,并比較每一對相鄰的元素,當(dāng)該對元素順序不正確時進(jìn)行交換。一直重復(fù)這個過程,直到?jīng)]有任何兩個相鄰元素可以交換,就表明完成了排序2022-09-09SpringBoot 快速實現(xiàn) api 加密的方法
在項目中,為了保證數(shù)據(jù)的安全,我們常常會對傳遞的數(shù)據(jù)進(jìn)行加密,常用的加密算法包括對稱加密(AES)和非對稱加密(RSA),本文給大家介紹SpringBoot 快速實現(xiàn) api 加密,感興趣的朋友一起看看吧2023-10-10解決Maven無法下載2.1.7.js7版本的itext依賴問題
本文主要解決使用Maven編譯項目時出現(xiàn)的itext依賴版本問題,通過分析,發(fā)現(xiàn)該問題是由jasperreports依賴的特定版本itext導(dǎo)致的,解決方法是排除jasperreports中的itext依賴,并自行指定更高版本的itext依賴2024-12-12