javascript
(转)Spring管理的Bean的生命周期
http://blog.csdn.net/yerenyuan_pku/article/details/52834011
bean的初始化時(shí)機(jī)
前面講解了Spring容器管理的bean的作用域。接著我們就要思考一個(gè)問題:bean到底是在什么時(shí)候才進(jìn)行實(shí)例化的呢?我們以這個(gè)問題為引子來展開本文的說明。?
bean對(duì)象無外乎是在以下兩個(gè)時(shí)刻進(jìn)行實(shí)例化的:
那么bean對(duì)象到底是在哪個(gè)時(shí)刻進(jìn)行實(shí)例化的,這與Bean的作用域有著某種聯(lián)系。我們以配置Spring管理的bean的作用域的案例為基礎(chǔ)進(jìn)行深入探討。為了能夠清楚地看到bean對(duì)象的實(shí)例化,我們需要修改PersonServiceBean類的代碼為:
public class PersonServiceBean implements PersonService { public PersonServiceBean() { System.out.println("我被實(shí)例化了"); } @Override public void save() { System.out.println("我是save()方法"); } }- 1
-
當(dāng)Spring的配置文件——beans.xml的內(nèi)容為:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"></bean> </beans>- 1
即bean的作用域?yàn)閟ingleton時(shí),我們修改SpringTest類的代碼為:
public class SpringTest {@Testpublic void test() { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); // 實(shí)例化Spring容器 } }- 1
此時(shí),測(cè)試test()方法,Eclipse控制臺(tái)輸出:
我被實(shí)例化了
這說明了當(dāng)bean的作用域?yàn)閟ingleton時(shí),bean對(duì)象是在Spring容器啟動(dòng)時(shí)就進(jìn)行創(chuàng)建了。即默認(rèn)情況下會(huì)在容器啟動(dòng)時(shí)初始化bean,但我們也可以指定bean節(jié)點(diǎn)的lazy-init=“true”來延遲初始化bean,這時(shí)候,只有第一次獲取bean會(huì)才初始化bean。如:?
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean" lazy-init="true"></bean> </beans>
我們將Spring的配置文件——beans.xml的內(nèi)容改為:- 1
此時(shí),測(cè)試test()方法,Eclipse控制臺(tái)根本就不會(huì)輸出這句話:
我被實(shí)例化了
lazy-init=”true”指定了不要在Spring容器啟動(dòng)時(shí)對(duì)這個(gè)bean進(jìn)行實(shí)例化。?
public class SpringTest {@Testpublic void test() { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); // 實(shí)例化Spring容器 PersonService personService = (PersonService) ctx.getBean("personService"); // 從Spring容器取得bean } }
這時(shí),只有將SpringTest類的代碼修改為:- 1
再次測(cè)試test()方法,Eclipse控制臺(tái)才會(huì)輸出這句話:
我被實(shí)例化了
如果想對(duì)所有bean都應(yīng)用延遲初始化,可以在根節(jié)點(diǎn)beans設(shè)置default-lazy-init=“true”,如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" default-lazy-init="true"> ...... </beans>- 1
-
當(dāng)Spring的配置文件——beans.xml的內(nèi)容為:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean" scope="prototype"></bean> </beans>- 1
即bean的作用域?yàn)閜rototype時(shí),若SpringTest類的代碼為:
public class SpringTest {@Testpublic void test() { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); // 實(shí)例化Spring容器 } }- 1
測(cè)試test()方法,可以發(fā)現(xiàn)Eclipse控制臺(tái)沒輸出這句話:
我被實(shí)例化了
這就說明了當(dāng)bean的作用域?yàn)閜rototype時(shí),bean對(duì)象并不會(huì)在Spring容器啟動(dòng)時(shí)就進(jìn)行創(chuàng)建。?
public class SpringTest {@Testpublic void test() { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); // 實(shí)例化Spring容器 PersonService personService = (PersonService) ctx.getBean("personService"); // 從Spring容器取得bean } }
但是若將SpringTest類的代碼改為:- 1
此時(shí),再測(cè)試test()方法,可以發(fā)現(xiàn)Eclipse控制臺(tái)輸出了這句話:
我被實(shí)例化了
證實(shí)了當(dāng)bean的作用域?yàn)閜rototype時(shí),bean對(duì)象將會(huì)在調(diào)用getBean()方法時(shí)進(jìn)行創(chuàng)建。
指定bean的初始化方法和銷毀方法
我們希望在bean被初始化的時(shí)候,就初始化某些資源。為了達(dá)到這樣的目的,我們可修改PersonServiceBean類的代碼為:
public class PersonServiceBean implements PersonService { public void init() { System.out.println("初始化某些資源"); } public PersonServiceBean() { System.out.println("我被實(shí)例化了"); } @Override public void save() { System.out.println("我是save()方法"); } }- 1
這樣,我們的目的就具體地成為:當(dāng)Spring容器初始化PersonServiceBean對(duì)象之后,就要執(zhí)行該對(duì)象的init()方法。為了達(dá)成這樣的目的,只須修改Spring的配置文件——beans.xml的內(nèi)容為:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean" lazy-init="false" init-method="init" /> </beans>- 1
- 2
若SpringTest類的代碼為:
public class SpringTest {@Testpublic void test() { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); // 實(shí)例化Spring容器 } }- 1
測(cè)試test()方法,Eclipse控制臺(tái)將打印:?
現(xiàn)在我們又希望在bean被銷毀的時(shí)候,就釋放或關(guān)閉某些資源。為了達(dá)到這樣的目的,我們可修改PersonServiceBean類的代碼為:
- 1
試著思考這樣一個(gè)問題:bean對(duì)象到底是什么時(shí)候銷毀的呢?答案是:如果沒有人為地刪除它,默認(rèn)該bean一直在Spring容器中,也就是說隨著Spring容器的關(guān)閉,該bean才會(huì)被銷毀。?
緊接著,我們要修改Spring的配置文件——beans.xml的內(nèi)容。
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
最后,我們要修改測(cè)試類——SpringTest.java的代碼為:
public class SpringTest {@Testpublic void test() { // ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); // 實(shí)例化Spring容器 AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); ctx.close(); // 正常關(guān)閉Spring容器 } }- 1
此時(shí),測(cè)試test()方法,Eclipse控制臺(tái)將打印:?
這就是Spring管理的Bean的生命周期。源碼可點(diǎn)擊Spring管理的Bean的生命周期進(jìn)行下載。
總結(jié)
以上是生活随笔為你收集整理的(转)Spring管理的Bean的生命周期的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Replica Sets+Shardin
- 下一篇: 排序的概念(选择排序1)