javascript
(转)Spring管理的Bean的生命周期
http://blog.csdn.net/yerenyuan_pku/article/details/52834011
bean的初始化時機
前面講解了Spring容器管理的bean的作用域。接著我們就要思考一個問題:bean到底是在什么時候才進行實例化的呢?我們以這個問題為引子來展開本文的說明。?
bean對象無外乎是在以下兩個時刻進行實例化的:
那么bean對象到底是在哪個時刻進行實例化的,這與Bean的作用域有著某種聯系。我們以配置Spring管理的bean的作用域的案例為基礎進行深入探討。為了能夠清楚地看到bean對象的實例化,我們需要修改PersonServiceBean類的代碼為:
public class PersonServiceBean implements PersonService { public PersonServiceBean() { System.out.println("我被實例化了"); } @Override public void save() { System.out.println("我是save()方法"); } }- 1
-
當Spring的配置文件——beans.xml的內容為:
<?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的作用域為singleton時,我們修改SpringTest類的代碼為:
public class SpringTest {@Testpublic void test() { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); // 實例化Spring容器 } }- 1
此時,測試test()方法,Eclipse控制臺輸出:
我被實例化了
這說明了當bean的作用域為singleton時,bean對象是在Spring容器啟動時就進行創建了。即默認情況下會在容器啟動時初始化bean,但我們也可以指定bean節點的lazy-init=“true”來延遲初始化bean,這時候,只有第一次獲取bean會才初始化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的內容改為:- 1
此時,測試test()方法,Eclipse控制臺根本就不會輸出這句話:
我被實例化了
lazy-init=”true”指定了不要在Spring容器啟動時對這個bean進行實例化。?
public class SpringTest {@Testpublic void test() { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); // 實例化Spring容器 PersonService personService = (PersonService) ctx.getBean("personService"); // 從Spring容器取得bean } }
這時,只有將SpringTest類的代碼修改為:- 1
再次測試test()方法,Eclipse控制臺才會輸出這句話:
我被實例化了
如果想對所有bean都應用延遲初始化,可以在根節點beans設置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
-
當Spring的配置文件——beans.xml的內容為:
<?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的作用域為prototype時,若SpringTest類的代碼為:
public class SpringTest {@Testpublic void test() { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); // 實例化Spring容器 } }- 1
測試test()方法,可以發現Eclipse控制臺沒輸出這句話:
我被實例化了
這就說明了當bean的作用域為prototype時,bean對象并不會在Spring容器啟動時就進行創建。?
public class SpringTest {@Testpublic void test() { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); // 實例化Spring容器 PersonService personService = (PersonService) ctx.getBean("personService"); // 從Spring容器取得bean } }
但是若將SpringTest類的代碼改為:- 1
此時,再測試test()方法,可以發現Eclipse控制臺輸出了這句話:
我被實例化了
證實了當bean的作用域為prototype時,bean對象將會在調用getBean()方法時進行創建。
指定bean的初始化方法和銷毀方法
我們希望在bean被初始化的時候,就初始化某些資源。為了達到這樣的目的,我們可修改PersonServiceBean類的代碼為:
public class PersonServiceBean implements PersonService { public void init() { System.out.println("初始化某些資源"); } public PersonServiceBean() { System.out.println("我被實例化了"); } @Override public void save() { System.out.println("我是save()方法"); } }- 1
這樣,我們的目的就具體地成為:當Spring容器初始化PersonServiceBean對象之后,就要執行該對象的init()方法。為了達成這樣的目的,只須修改Spring的配置文件——beans.xml的內容為:
<?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"); // 實例化Spring容器 } }- 1
測試test()方法,Eclipse控制臺將打印:?
現在我們又希望在bean被銷毀的時候,就釋放或關閉某些資源。為了達到這樣的目的,我們可修改PersonServiceBean類的代碼為:
- 1
試著思考這樣一個問題:bean對象到底是什么時候銷毀的呢?答案是:如果沒有人為地刪除它,默認該bean一直在Spring容器中,也就是說隨著Spring容器的關閉,該bean才會被銷毀。?
緊接著,我們要修改Spring的配置文件——beans.xml的內容。
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
最后,我們要修改測試類——SpringTest.java的代碼為:
public class SpringTest {@Testpublic void test() { // ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); // 實例化Spring容器 AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); ctx.close(); // 正常關閉Spring容器 } }- 1
此時,測試test()方法,Eclipse控制臺將打印:?
這就是Spring管理的Bean的生命周期。源碼可點擊Spring管理的Bean的生命周期進行下載。
總結
以上是生活随笔為你收集整理的(转)Spring管理的Bean的生命周期的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Replica Sets+Shardin
- 下一篇: gradle idea java ssm