淺談Spring Bean的作用域之間有什么區(qū)別
前言
Spring的 bean有5種作用域分別是:singleton、prototype、request、session和globalSession
Spring Bean的作用域之間有什么區(qū)別?
? 在Spring中,可以在<bean>元素的scope屬性里設(shè)置bean的作用域,以決定這個(gè)bean是單例的還是多例的。
? 默認(rèn)情況下,Spring只為每個(gè)在IOC容器里聲明的bean創(chuàng)建唯一一個(gè)實(shí)例,整個(gè)IOC容器范圍內(nèi)都能共享該實(shí)例:所有后續(xù)的getBean()調(diào)用和bean引用都將返回這個(gè)唯一的bean實(shí)例。該作用域稱為singleton,它是bean的默認(rèn)作用域。
作用域的類別跟說明
- singleton:在SpringIOC容器中僅存在一個(gè)Bean實(shí)例,Bean以單實(shí)例的方式存在
- prototype:每次調(diào)用getBean()時(shí)都會(huì)返回一個(gè)新的實(shí)例
- request:每次HTTP請(qǐng)求都會(huì)創(chuàng)建一個(gè)新的Bean,該作用域僅適用于WebApplicationContext環(huán)境
- session:同一個(gè)HTTP Session共享一個(gè)Bean,不同的HTTP Session使用不同的Bean,該作用域僅適用于WebApplicationContext環(huán)境
簡(jiǎn)述
bean的作用域:可以通過元素的scope屬性來指定bean作用域
- singleton:默認(rèn)值。當(dāng)IOC容器一創(chuàng)建就會(huì)創(chuàng)建bean的實(shí)例,而且是單例的,每次得到的都是同一個(gè)
- prototype:原型的。當(dāng)IOC容器一創(chuàng)建不再實(shí)例化該bean,每次調(diào)用getBean方法時(shí)再實(shí)例化該bean,而且每調(diào)用
- request:每次請(qǐng)求實(shí)例化一個(gè)bean
- session:在一次會(huì)話中共享一個(gè)bean
測(cè)試用例
因?yàn)槠綍r(shí)使用SPRING MVC開發(fā)的時(shí)候比較多,有必要了解清楚怎么去調(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是單例的,因此必須通過實(shí)現(xiàn)ApplicationContextAware接口,直接從容器中取出對(duì)象。
因此測(cè)試的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. 運(yùn)行結(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的級(jí)別就是單例);
prototype的bean每次的數(shù)據(jù)都是不一樣的,每次請(qǐng)求的時(shí)候調(diào)用兩次結(jié)果都不一樣。
request的bean在每次request的時(shí)候都不一致,但是同一次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,沒試驗(yàn),據(jù)說是在多個(gè)session之間可以共享,效果等同于全局變量。
到此這篇關(guān)于淺談Spring Bean的作用域之間有什么區(qū)別的文章就介紹到這了,更多相關(guān)Spring Bean作用域內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實(shí)現(xiàn)經(jīng)典游戲之大魚吃小魚
這篇文章主要為大家詳細(xì)介紹了如何利用Java語言實(shí)現(xiàn)經(jīng)典游戲之大魚吃小魚,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Java游戲開發(fā)有一定幫助,需要的可以參考一下2022-08-08Resty開發(fā)restful版本的Jfinal深入研究
這篇文章主要為大家介紹了Resty開發(fā)restful版本的Jfinal深入研究有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-03-03maven打包web項(xiàng)目時(shí)同時(shí)打包為war和jar文件的方法
本篇文章主要介紹了maven打包web項(xiàng)目時(shí)同時(shí)打包為war和jar文件的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10圖解Java經(jīng)典算法冒泡選擇插入希爾排序的原理與實(shí)現(xiàn)
冒泡排序是一種簡(jiǎn)單的排序算法,它也是一種穩(wěn)定排序算法。其實(shí)現(xiàn)原理是重復(fù)掃描待排序序列,并比較每一對(duì)相鄰的元素,當(dāng)該對(duì)元素順序不正確時(shí)進(jìn)行交換。一直重復(fù)這個(gè)過程,直到?jīng)]有任何兩個(gè)相鄰元素可以交換,就表明完成了排序2022-09-09基于RabbitMQ的簡(jiǎn)單應(yīng)用(詳解)
下面小編就為大家分享一篇基于RabbitMQ的簡(jiǎn)單應(yīng)用(詳解),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2017-11-11SpringBoot 快速實(shí)現(xiàn) api 加密的方法
在項(xiàng)目中,為了保證數(shù)據(jù)的安全,我們常常會(huì)對(duì)傳遞的數(shù)據(jù)進(jìn)行加密,常用的加密算法包括對(duì)稱加密(AES)和非對(duì)稱加密(RSA),本文給大家介紹SpringBoot 快速實(shí)現(xiàn) api 加密,感興趣的朋友一起看看吧2023-10-10struts2的流程和一系列相關(guān)知識(shí)代碼解析
這篇文章主要介紹了struts2的流程和一系列相關(guān)知識(shí)代碼解析,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12解決Maven無法下載2.1.7.js7版本的itext依賴問題
本文主要解決使用Maven編譯項(xiàng)目時(shí)出現(xiàn)的itext依賴版本問題,通過分析,發(fā)現(xiàn)該問題是由jasperreports依賴的特定版本itext導(dǎo)致的,解決方法是排除jasperreports中的itext依賴,并自行指定更高版本的itext依賴2024-12-12