簡單了解spring bean作用域?qū)傩詓ingleton和prototype的區(qū)別
這篇文章主要介紹了簡單了解spring bean作用域?qū)傩詓ingleton和prototype的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
1.singleton
當(dāng)一個(gè)bean的作用域設(shè)置為singleton, 那么Spring IOC容器中只會存在一個(gè)共享的bean實(shí)例,并且所有對bean的請求,只要id與該bean定義相匹配,則只會返回bean的同一實(shí)例。
換言之,當(dāng)把一個(gè)bean定義設(shè)置為singleton作用域時(shí),Spring IOC容器只會創(chuàng)建該bean定義的唯一實(shí)例。這個(gè)單一實(shí)例會被存儲到單例緩存(singleton cache)中,并且所有針對該bean的后續(xù)請求和引用都將返回被緩存的對象實(shí)例,這里要注意的是singleton作用域和GOF設(shè)計(jì)模式中的單例是完全不同的,單例設(shè)計(jì)模式表示一個(gè)ClassLoader中只有一個(gè)class存在,而這里的singleton則表示一個(gè)容器對應(yīng)一個(gè)bean,也就是說當(dāng)一個(gè)bean被標(biāo)識為singleton時(shí)候,spring的IOC容器中只會存在一個(gè)該bean。
applicationContextER.xml:
<!--Spring bean作用域--> <bean id="get_date" class="java.util.Date" scope="singleton"/>
測試代碼:
public class GetDate { public static void main(String[] args){ //獲取應(yīng)用程序上下文接口 ApplicationContext apl = new ClassPathXmlApplicationContext("applicationContextER.xml"); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { //反復(fù)調(diào)用getBean來查看時(shí)間 Date date = (Date) apl.getBean("get_date"); //休息3秒 Thread.sleep(1000); System.out.println("--------------:" + simpleDateFormat.format(date)); Date date1 = (Date) apl.getBean("get_date"); Thread.sleep(1000); System.out.println("--------------:" + simpleDateFormat.format(date1)); Date date2 = (Date) apl.getBean("get_date"); Thread.sleep(1000); System.out.println("--------------:" + simpleDateFormat.format(date2)); System.out.println("date is date1 : " + (date == date1)); System.out.println("date1 is date2 : " + (date1 == date2)); } catch (Exception e) { } } }
測試結(jié)果:
23:05:04.298 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'get_date' 23:05:04.298 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'get_date' 23:05:04.308 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'get_date' to allow for resolving potential circular references 23:05:04.309 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'get_date' 23:05:04.310 [main] DEBUG org.springframework.context.support.ClassPathXmlApplicationContext - Unable to locate LifecycleProcessor with name 'lifecycleProcessor': using default [org.springframework.context.support.DefaultLifecycleProcessor@3108bc] 23:05:04.310 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'lifecycleProcessor' 23:05:04.311 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Could not find key 'spring.liveBeansView.mbeanDomain' in any property source 23:05:04.316 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'get_date' --------------:2019-12-21 23:05:04 23:05:05.320 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'get_date' --------------:2019-12-21 23:05:04 23:05:06.324 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'get_date' --------------:2019-12-21 23:05:04 date is date1 : true date1 is date2 : true
從上面的結(jié)果可以看出,創(chuàng)建好對象之后,存入了緩存中。后面每次都是獲取的對象都是從緩存中獲取的,而不是新創(chuàng)建的。所以每次獲取的對象都是一樣的。
2.prototype
prototype作用域部署的bean,每一次請求(將其注入到另一個(gè)bean中,或者以程序的方式調(diào)用容器的getBean()方法)都會產(chǎn)生一個(gè)新的bean實(shí)例,相當(dāng)與一個(gè)new的操作,對于prototype作用域的bean,有一點(diǎn)非常重要,那就是Spring不能對一個(gè)prototype bean的整個(gè)生命周期負(fù)責(zé),容器在初始化、配置、裝飾或者是裝配完一個(gè)prototype實(shí)例后,將它交給客戶端,隨后就對該prototype實(shí)例不聞不問了。
不管何種作用域,容器都會調(diào)用所有對象的初始化生命周期回調(diào)方法,而對prototype而言,任何配置好的析構(gòu)生命周期回調(diào)方法都將不會被調(diào)用。清除prototype作用域的對象并釋放任何prototype bean所持有的昂貴資源,都是客戶端代碼的職責(zé)。(讓Spring容器釋放被singleton作用域bean占用資源的一種可行方式是,通過使用bean的后置處理器,該處理器持有要被清除的bean的引用。
applicationContextER.xml:
<!--Spring bean作用域--> <bean id="get_date" class="java.util.Date" scope="prototype"/>
測試結(jié)果:
23:01:51.314 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'get_date' 23:01:51.324 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'get_date' --------------:2019-12-21 23:01:51 23:01:52.329 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'get_date' 23:01:52.329 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'get_date' --------------:2019-12-21 23:01:52 23:01:53.330 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'get_date' 23:01:53.331 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'get_date' --------------:2019-12-21 23:01:53 date is date1 : false date1 is date2 : false
從上面的結(jié)果可以看出,每次都是創(chuàng)建一個(gè)新對象,所以每次的對象都不一樣。
總結(jié):從1和2可以看出,當(dāng)你需要全局的唯一標(biāo)示的時(shí)候可以用singleton,而且singleton只創(chuàng)建一個(gè)對象,系統(tǒng)消耗資源小.但是用singleton可能會有線程安全化的問題,這個(gè)時(shí)候就需要用到prototype ??紤]并發(fā)的問題,建議都用prototype。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:SpringBoot項(xiàng)目的測試類實(shí)例解析
欄 目:Java
本文標(biāo)題:簡單了解spring bean作用域?qū)傩詓ingleton和prototype的區(qū)別
本文地址:http://mengdiqiu.com.cn/a1/Java/8876.html
您可能感興趣的文章
- 01-10Springboot中@Value的使用詳解
- 01-10springboot實(shí)現(xiàn)文件上傳步驟解析
- 01-10springboot jta atomikos實(shí)現(xiàn)分布式事物管理
- 01-10java判斷是否空最簡單的方法
- 01-10SpringBoot使用RabbitMQ延時(shí)隊(duì)列(小白必備)
- 01-10如何基于SpringBoot部署外部Tomcat過程解析
- 01-10SPRING BOOT啟動命令參數(shù)及源碼詳析
- 01-10springboot集成fastDfs過程代碼實(shí)例
- 01-10springmvc級聯(lián)屬性處理無法轉(zhuǎn)換異常問題解決
- 01-10SPRINGBOOT讀取PROPERTIES配置文件數(shù)據(jù)過程詳解


閱讀排行
本欄相關(guān)
- 01-10Java實(shí)現(xiàn)動態(tài)模擬時(shí)鐘
- 01-10Springboot中@Value的使用詳解
- 01-10JavaWeb實(shí)現(xiàn)郵件發(fā)送功能
- 01-10利用Java實(shí)現(xiàn)復(fù)制Excel工作表功能
- 01-10Java實(shí)現(xiàn)動態(tài)數(shù)字時(shí)鐘
- 01-10java基于poi導(dǎo)出excel透視表代碼實(shí)例
- 01-10java實(shí)現(xiàn)液晶數(shù)字字體顯示當(dāng)前時(shí)間
- 01-10基于Java驗(yàn)證jwt token代碼實(shí)例
- 01-10Java動態(tài)顯示當(dāng)前日期和時(shí)間
- 01-10淺談Java中真的只有值傳遞么
隨機(jī)閱讀
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10delphi制作wav文件的方法
- 04-02jquery與jsp,用jquery