詳解SpringIOC容器中bean的作用范圍和生命周期
bean的作用范圍:
可以通過scope屬性進(jìn)行設(shè)置:
- singleton 單例的(默認(rèn))
- prototype 多例的
- request 作用于web應(yīng)用的請求范圍
- session 作用于web應(yīng)用的會話范圍
- global-session 作用于集群環(huán)境的會話范圍(全局會話范圍)
測試:
<!-- 默認(rèn)是單例的(singleton)--> <bean id="human" class="com.entity.Human"></bean>
<bean id="human" class="com.entity.Human" scope="singleton"></bean>
@Test public void test(){ //通過ClassPathXmlApplicationContext對象加載配置文件方式將javabean對象交給spring來管理 ApplicationContext applicationContext=new ClassPathXmlApplicationContext("bean.xml"); //獲取Spring容器中的bean對象,通過id和類字節(jié)碼來獲取 Human human = applicationContext.getBean("human", Human.class); Human human1 = applicationContext.getBean("human", Human.class); System.out.println(human==human1); }
結(jié)果:
將scope屬性設(shè)置為prototype時
<bean id="human" class="com.entity.Human" scope="prototype"></bean>
結(jié)果:
singleton和prototype的區(qū)別
- 如果bean屬性設(shè)置為singleton時,當(dāng)我們加載配置文件時對象已經(jīng)被初始化
- 而如果使用prototype時,對象的創(chuàng)建是我們什么時候獲取bean時什么時候創(chuàng)建對象
當(dāng)設(shè)置為prototype時
當(dāng)設(shè)置為singleton時
bean對象的生命周期
單例對象:
- 出生:當(dāng)容器創(chuàng)建時對象出生
- 活著:只有容器還在,對象一直活著
- 死亡:容器銷戶,對象死亡
- 單例對象和容器生命周期相同
測試:
先設(shè)置屬性init-method和destroy-method,同時在person類中寫入兩個方法進(jìn)行輸出打印
public void init(){ System.out.println("初始化..."); } public void destroy(){ System.out.println("銷毀了..."); }
<bean id="person" class="com.entity.Person" scope="singleton" init-method="init" destroy-method="destroy"> </bean>
測試類:
@Test public void test(){ //通過ClassPathXmlApplicationContext對象加載配置文件方式將javabean對象交給spring來管理 ClassPathXmlApplicationContext Context=new ClassPathXmlApplicationContext("bean.xml"); // //獲取Spring容器中的bean對象,通過id和類字節(jié)碼來獲取 Person person = Context.getBean("person", Person.class); //銷毀容器 Context.close(); }
結(jié)果:
總結(jié):單例對象和容器生命周期相同
當(dāng)屬性改為prototype多例時
- 出生:當(dāng)我們使用對象時spring框架為我們創(chuàng)建
- 活著:對象只要是在使用過程中就一直活著
- 死亡:當(dāng)對象長時間不用,且沒有別的對象應(yīng)用時,由java垃圾回收器回收對象
測試類:
@Test public void test(){ //通過ClassPathXmlApplicationContext對象加載配置文件方式將javabean對象交給spring來管理 ClassPathXmlApplicationContext Context=new ClassPathXmlApplicationContext("bean.xml"); // //獲取Spring容器中的bean對象,通過id和類字節(jié)碼來獲取 Person person = Context.getBean("person", Person.class); //銷毀容器 Context.close(); }
結(jié)果:
總結(jié):由于Spring容器不知道多例對象什么時候使用,什么時候能用完,只有我們自己知道,因此它不會輕易的把對象銷毀,它會通過java垃圾回收器回收對象
到此這篇關(guān)于SpringIOC容器中bean的作用范圍和生命周期的文章就介紹到這了,更多相關(guān)SpringIOC容器bean作用范圍和生命周期內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
RocketMQ源碼解析topic創(chuàng)建機制詳解
這篇文章主要為大家介紹了RocketMQ源碼解析topic創(chuàng)建機制詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08在lambda的foreach遍歷中break退出操作(lambda foreach break)
這篇文章主要介紹了在lambda的foreach遍歷中break退出操作(lambda foreach break),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09Java 中使用數(shù)組存儲和操作數(shù)據(jù)
本文將介紹Java中常用的數(shù)組操作方法,通過詳細(xì)的示例和解釋,幫助讀者全面理解和掌握這些方法,具有一定的參考價值,感興趣的可以了解一下2023-09-09Java設(shè)計模式之責(zé)任鏈模式(Chain of Responsibility模式)介紹
這篇文章主要介紹了Java設(shè)計模式之責(zé)任鏈模式(Chain of Responsibility模式)介紹,本文講解了如何使用責(zé)任鏈模式,并給出了4種使用實例,需要的朋友可以參考下2015-03-03MyBatis Plus整合Redis實現(xiàn)分布式二級緩存的問題
Mybatis內(nèi)置的二級緩存在分布式環(huán)境下存在分布式問題,無法使用,但是我們可以整合Redis來實現(xiàn)分布式的二級緩存,這篇文章給大家介紹MyBatis Plus整合Redis實現(xiàn)分布式二級緩存,感興趣的朋友跟隨小編一起看看吧2023-11-11