簡(jiǎn)單了解spring bean作用域?qū)傩詓ingleton和prototype的區(qū)別
這篇文章主要介紹了簡(jiǎn)單了解spring bean作用域?qū)傩詓ingleton和prototype的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
1.singleton
當(dāng)一個(gè)bean的作用域設(shè)置為singleton, 那么Spring IOC容器中只會(huì)存在一個(gè)共享的bean實(shí)例,并且所有對(duì)bean的請(qǐng)求,只要id與該bean定義相匹配,則只會(huì)返回bean的同一實(shí)例。
換言之,當(dāng)把一個(gè)bean定義設(shè)置為singleton作用域時(shí),Spring IOC容器只會(huì)創(chuàng)建該bean定義的唯一實(shí)例。這個(gè)單一實(shí)例會(huì)被存儲(chǔ)到單例緩存(singleton cache)中,并且所有針對(duì)該bean的后續(xù)請(qǐng)求和引用都將返回被緩存的對(duì)象實(shí)例,這里要注意的是singleton作用域和GOF設(shè)計(jì)模式中的單例是完全不同的,單例設(shè)計(jì)模式表示一個(gè)ClassLoader中只有一個(gè)class存在,而這里的singleton則表示一個(gè)容器對(duì)應(yīng)一個(gè)bean,也就是說當(dāng)一個(gè)bean被標(biāo)識(shí)為singleton時(shí)候,spring的IOC容器中只會(huì)存在一個(gè)該bean。
applicationContextER.xml:
<!--Spring bean作用域--> <bean id="get_date" class="java.util.Date" scope="singleton"/>
測(cè)試代碼:
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) { } } }
測(cè)試結(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)建好對(duì)象之后,存入了緩存中。后面每次都是獲取的對(duì)象都是從緩存中獲取的,而不是新創(chuàng)建的。所以每次獲取的對(duì)象都是一樣的。
2.prototype
prototype作用域部署的bean,每一次請(qǐng)求(將其注入到另一個(gè)bean中,或者以程序的方式調(diào)用容器的getBean()方法)都會(huì)產(chǎn)生一個(gè)新的bean實(shí)例,相當(dāng)與一個(gè)new的操作,對(duì)于prototype作用域的bean,有一點(diǎn)非常重要,那就是Spring不能對(duì)一個(gè)prototype bean的整個(gè)生命周期負(fù)責(zé),容器在初始化、配置、裝飾或者是裝配完一個(gè)prototype實(shí)例后,將它交給客戶端,隨后就對(duì)該prototype實(shí)例不聞不問了。
不管何種作用域,容器都會(huì)調(diào)用所有對(duì)象的初始化生命周期回調(diào)方法,而對(duì)prototype而言,任何配置好的析構(gòu)生命周期回調(diào)方法都將不會(huì)被調(diào)用。清除prototype作用域的對(duì)象并釋放任何prototype bean所持有的昂貴資源,都是客戶端代碼的職責(zé)。(讓Spring容器釋放被singleton作用域bean占用資源的一種可行方式是,通過使用bean的后置處理器,該處理器持有要被清除的bean的引用。
applicationContextER.xml:
<!--Spring bean作用域--> <bean id="get_date" class="java.util.Date" scope="prototype"/>
測(cè)試結(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è)新對(duì)象,所以每次的對(duì)象都不一樣。
總結(jié):從1和2可以看出,當(dāng)你需要全局的唯一標(biāo)示的時(shí)候可以用singleton,而且singleton只創(chuàng)建一個(gè)對(duì)象,系統(tǒng)消耗資源小.但是用singleton可能會(huì)有線程安全化的問題,這個(gè)時(shí)候就需要用到prototype ??紤]并發(fā)的問題,建議都用prototype。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 詳解Spring中Bean的生命周期和作用域及實(shí)現(xiàn)方式
- 淺談Spring中Bean的作用域、生命周期
- Spring實(shí)戰(zhàn)之Bean的作用域request用法分析
- Spring實(shí)戰(zhàn)之Bean的作用域singleton和prototype用法分析
- 深入了解Spring中Bean的作用域和生命周期
- spring ioc的簡(jiǎn)單實(shí)例及bean的作用域?qū)傩越馕?/a>
- JSP 中Spring Bean 的作用域詳解
- Spring中Bean的作用域與生命周期詳解
- 詳解Spring中Bean的作用域與生命周期
- Spring?框架中的?Bean?作用域(Scope)使用詳解
相關(guān)文章
java實(shí)現(xiàn)左旋轉(zhuǎn)字符串
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)左旋轉(zhuǎn)字符串,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03java客戶端Etcd官方倉庫jetcd中KeepAlive接口實(shí)現(xiàn)
這篇文章主要為大家介紹了java客戶端Etcd官方倉庫jetcd中KeepAlive接口實(shí)現(xiàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,多多加薪2022-02-02SpringBoot使用AOP與注解實(shí)現(xiàn)請(qǐng)求參數(shù)自動(dòng)填充流程詳解
面向切面編程(aspect-oriented programming,AOP)主要實(shí)現(xiàn)的目的是針對(duì)業(yè)務(wù)處理過程中的切面進(jìn)行提取,諸如日志、事務(wù)管理和安全這樣的系統(tǒng)服務(wù),從而使得業(yè)務(wù)邏輯各部分之間的耦合度降低,提高程序的可重用性,同時(shí)提高了開發(fā)的效率2023-02-02SpringBoot?SpringSecurity?詳細(xì)介紹(基于內(nèi)存的驗(yàn)證)
這篇文章主要介紹了SpringBoot?SpringSecurity?介紹(基于內(nèi)存的驗(yàn)證),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04解決Lombok使用@Builder無法build父類屬性的問題
這篇文章主要介紹了解決Lombok使用@Builder無法build父類屬性的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09Spring boot + mybatis + orcale實(shí)現(xiàn)步驟實(shí)例代碼講解
這篇文章主要介紹了Spring boot + mybatis + orcale的實(shí)現(xiàn)步驟實(shí)例代碼講解,需要的朋友可以參考下2017-12-12