Bean的生命周期:
Bean的定義——Bean的初始化——Bean的使用——Bean的銷毀
Bean的定義
Bean 是 spring 裝配的組件模型,一切實體類都可以配置成一個 Bean ,進而就可以在任何其他的 Bean 中使用,一個 Bean 也可以不是指定的實體類,這就是抽象 Bean 。
Bean的初始化
Spring中bean的初始化回調(diào)有兩種方法
一種是在配置文件中聲明init-method="init",然后在一個實體類中用init()方法來初始化
另一種是實現(xiàn)InitializingBean接口,覆蓋afterPropertiesSet()方法。
第一種:
配置文件:
1
2
3
4
5
6
7
8
9
10
11
12
|
<? xml version = "1.0" encoding = "UTF-8" ?> < beans xmlns = "http://www.springframework.org/schema/beans" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:p = "http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> < bean id = "init-one" class = "org.spring.test.BeanInitDemo1" init-method = "init" > < property name = "message" value = "這里是配置文件中為message賦值" ></ property > </ bean > </ beans > |
BeanInitDemo1類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
package org.spring.test; public class BeanInitDemo1 { private String message; public String getMessage() { return message; } public void setMessage(String message) { this .message = message; } public void init(){ this .setMessage( "這里是init()方法初始化設(shè)值" ); } } |
測試類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
package org.spring.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml" ); BeanInitDemo1 bid = (BeanInitDemo1) context.getBean( "init-one" ); System.out.println(bid.getMessage()); } } |
運行結(jié)果:
這里是init()方法初始化設(shè)值
原因:init()初始化方法的調(diào)用是在配置文件的Bean初始化之后執(zhí)行的, 所以改變了配置文件中對message的賦值。
第二種:
配置文件:
1
2
3
4
5
6
7
8
9
10
11
12
|
<? xml version = "1.0" encoding = "UTF-8" ?> < beans xmlns = "http://www.springframework.org/schema/beans" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:p = "http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> < bean id = "init-two" class = "org.spring.test.BeanInitDemo2" > < property name = "message" value = "這里是配置文件中為message賦值" ></ property > </ bean > </ beans > |
編寫B(tài)eanInitDemo2類,使其實現(xiàn)InitializingBean接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
package org.spring.test; import org.springframework.beans.factory.InitializingBean; public class BeanInitDemo2 implements InitializingBean{ private String message; public String getMessage() { return message; } public void setMessage(String message) { this .message = message; } public void afterPropertiesSet() throws Exception { // TODO Auto-generated method stub this .setMessage( "這里覆蓋了InitializingBean接口的afterPropertiesSet()方法設(shè)值" ); } } |
測試:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
package org.spring.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml" ); BeanInitDemo2 bid = (BeanInitDemo2) context.getBean( "init-two" ); System.out.println(bid.getMessage()); } } |
運行結(jié)果: 這里覆蓋了InitializingBean接口的afterPropertiesSet()方法設(shè)值
原因相同,afterPropertiesSet()方法在配置文件的Bean初始化后執(zhí)行,所以改變了配置文件中對message的賦值
Bean的使用
Spring中有兩種使用bean的方法:
1, BeanFactory:
1
2
|
BeanFactory factory= new XmlBeanFactory( new ClassPathResource( "bean.xml" )); factory.getBean( "student" ); |
BeanFactory是延遲加載,如果Bean的某一個屬性沒有注入,BeanFacotry加載后,直至第一次使用getBean方法才會拋出異常,也就是說當使用BeanFactory實例化對象時,配置的bean不會馬上被實例化。當你使用該bean時才會被實例化(getBean)。
2, ApplicationContext:
1
|
ApplicationContext ac = new ClassPathXmlApplicationContext( "bean.xml" ); |
如果使用ApplicationContext,則配置的bean如果是singleton不管你用還是不用,都被實例化。ApplicationContext在初始化自身時檢驗,這樣有利于檢查所依賴屬性是否注入。ApplicationContext是BeanFactory的子類,除了具有BeanFactory的所有功能外還提供了更完整的框架功能,例如國際化,資源訪問等。所以通常情況下我們選擇使用ApplicationContext。
Bean的銷毀
Bean的銷毀和初始化一樣,都是提供了兩個方法
一是在配置文件中聲明destroy-method="cleanup",然后在類中寫一個cleanup()方法銷毀
二是實現(xiàn)DisposableBean接口,覆蓋destory()方法
第一種:
配置文件:
1
2
3
4
5
6
7
8
9
10
11
12
|
<? xml version = "1.0" encoding = "UTF-8" ?> < beans xmlns = "http://www.springframework.org/schema/beans" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:p = "http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> < bean id = "destory-one" class = "org.spring.test.BeanDestoryDemo1" destroy-method = "cleanup" > < property name = "message" value = "這里是配置文件中為message賦值" ></ property > </ bean > </ beans > |
BeanDestoryDemo1類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
package org.spring.test; public class BeanDestoryDemo1 { private String message; public String getMessage() { return message; } public void setMessage(String message) { this .message = message; } public void cleanup(){ System.out.println( "銷毀之前可以調(diào)用一些方法" ); } } |
測試:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
package org.spring.test; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class DestortTest { public static void main(String[] args) { AbstractApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml" ); BeanDestoryDemo1 bdd = (BeanDestoryDemo1) context.getBean( "destory-one" ); System.out.println(bdd.getMessage()); context.registerShutdownHook(); } } |
運行結(jié)果:
context.registerShutdownHook()是為spring注冊關(guān)閉吊鉤,程序退出之前關(guān)閉spring容器,如果沒有
context.registerShutdownHook();將不會執(zhí)行cleanup()方法。
第二種:
配置文件:
1
2
3
4
5
6
7
8
9
10
11
12
|
<? xml version = "1.0" encoding = "UTF-8" ?> < beans xmlns = "http://www.springframework.org/schema/beans" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:p = "http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> < bean id = "destory-two" class = "org.spring.test.BeanDestoryDemo2" > < property name = "message" value = "這里是配置文件中為message賦值" ></ property > </ bean > </ beans > |
BeanDestoryDemo2類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
package org.spring.test; import org.springframework.beans.factory.DisposableBean; public class BeanDestoryDemo2 implements DisposableBean{ private String message; public String getMessage() { return message; } public void setMessage(String message) { this .message = message; } public void destroy() throws Exception { // TODO Auto-generated method stub System.out.println( "同樣,銷毀之前調(diào)用的方法" ); } } |
測試:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
package org.spring.test; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class DestortTest { public static void main(String[] args) { AbstractApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml" ); BeanDestoryDemo2 bdd = (BeanDestoryDemo2) context.getBean( "destory-two" ); System.out.println(bdd.getMessage()); context.registerShutdownHook(); } } |
運行結(jié)果:
Spring可以管理singleton作用域的Bean的生命周期,所以在Bean初始化及銷毀之前可以做一些工作,更靈活的管理Bean。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://blog.csdn.net/weixin_36380516/article/details/72354668?utm_source=tuicool&utm_medium=referral