前言
上一篇文章主要學習了下bean的配置、注入、自定義屬性編輯器,今天來熟悉bean的生命周期。
任何一個事物都有自己的生命周期,生命的開始、生命中、生命結束。大家最熟悉的應該是servlet 的生命周期吧。和 servlet 一樣 spring bean 也有自己的生命周期。
在開發中生命周期是一個很常見的名詞,基本每種編程語言都能找到與它關聯的。關于bean的生命周期我在網上也找了好多,基本都差不多。這里我主要是想通過代碼來驗證,畢竟學的知識都是一樣的,都是學的java,最重要的是動手練習,這樣印象更深。
下面是它生命周期的描述,我們通過demo來進行驗證。
下圖是它執行的順序。
一、創建liftcycle類實現beanfactoryaware,beannameaware,initializingbean,disposablebean,applicationcontextaware5個接口方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
package cuiyw.spring.service; import org.springframework.beans.beansexception; import org.springframework.beans.factory.beanfactory; import org.springframework.beans.factory.beanfactoryaware; import org.springframework.beans.factory.beannameaware; import org.springframework.beans.factory.disposablebean; import org.springframework.beans.factory.initializingbean; import org.springframework.context.applicationcontext; import org.springframework.context.applicationcontextaware; public class lifecycle implements beanfactoryaware,beannameaware,initializingbean,disposablebean,applicationcontextaware{ private string name; public string getname() { system.out.println( "getname name=" +name); return name; } public void setname(string name) { system.out.println( "setname name=" +name); this .name = name; } public void afterpropertiesset() throws exception { // todo auto-generated method stub system.out.println( "initializingbean.afterpropertiesset()" ); } public void setbeanname(string arg0) { // todo auto-generated method stub system.out.println( "beannameaware.setbeanname" ); } public void setbeanfactory(beanfactory arg0) throws beansexception { // todo auto-generated method stub system.out.println( "beanfactoryaware.setbeanfactory" ); } public void destroy() throws exception { // todo auto-generated method stub system.out.println( "disposablebean.destroy" ); } public void myinit() { system.out.println( "【init-method】調用<bean>的init-method屬性指定的初始化方法" ); } public void mydestory() { system.out.println( "【destroy-method】調用<bean>的destroy-method屬性指定的初始化方法" ); } public void setapplicationcontext(applicationcontext arg0) throws beansexception { // todo auto-generated method stub system.out.println( "applicationcontextaware.setapplicationcontext" ); } } |
二、注冊instantiationawarebeanpostprocessor接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
package cuiyw.spring.service; import java.beans.propertydescriptor; import org.springframework.beans.beansexception; import org.springframework.beans.propertyvalues; import org.springframework.beans.factory.config.instantiationawarebeanpostprocessor; public class myinstantiationawarebeanpostprocessor implements instantiationawarebeanpostprocessor{ public object postprocessafterinitialization(object bean, string beanname) throws beansexception { // todo auto-generated method stub system.out.println( "instantiationawarebeanpostprocessor.postprocessafterinitialization" ); return bean; } public object postprocessbeforeinitialization(object bean, string beanname) throws beansexception { // todo auto-generated method stub system.out.println( "instantiationawarebeanpostprocessor.postprocessbeforeinitialization" ); return bean; } public boolean postprocessafterinstantiation(object bean, string beanname) throws beansexception { // todo auto-generated method stub system.out.println( "instantiationawarebeanpostprocessor.postprocessafterinstantiation" ); return true ; } public object postprocessbeforeinstantiation( class <?> beanclass, string beanname) throws beansexception { // todo auto-generated method stub system.out.println( "instantiationawarebeanpostprocessor.postprocessbeforeinstantiation" ); return null ; } public propertyvalues postprocesspropertyvalues(propertyvalues pvs, propertydescriptor[] pds, object bean, string beanname) throws beansexception { // todo auto-generated method stub system.out.println( "instantiationawarebeanpostprocessor.postprocesspropertyvalues" ); return pvs; } } |
三、注冊beanpostprocessor接口
其實instantiationawarebeanpostprocessor繼承beanpostprocessor,所以在上面我也實現了beanpostprocessor接口的方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
package cuiyw.spring.service; import org.springframework.beans.beansexception; import org.springframework.beans.factory.config.beanpostprocessor; public class mybeanpostprocessor implements beanpostprocessor { public object postprocessafterinitialization(object bean, string beanname) throws beansexception { // todo auto-generated method stub system.out.println( "beanpostprocessor.postprocessafterinitialization " ); return bean; } public object postprocessbeforeinitialization(object bean, string beanname) throws beansexception { // todo auto-generated method stub system.out.println( "beanpostprocessor.postprocessbeforeinitialization" ); return bean; } } |
四、注冊beanfactorypostprocessor接口
1
2
3
4
5
6
7
8
9
10
|
package cuiyw.spring.service; import org.springframework.beans.beansexception; import org.springframework.beans.factory.config.beanfactorypostprocessor; import org.springframework.beans.factory.config.configurablelistablebeanfactory; public class mybeanfactorypostprocessor implements beanfactorypostprocessor { public void postprocessbeanfactory(configurablelistablebeanfactory arg0) throws beansexception { // todo auto-generated method stub system.out.println( "beanfactorypostprocessor.postprocessbeanfactory" ); } } |
五、在上下文中配置
這里還是在上一個博客demo的基礎上進行修改,為了有其他干擾,我先把service去掉了。
1
2
3
4
5
6
7
8
9
10
11
|
<?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= "beanpostprocessor" class = "cuiyw.spring.service.mybeanpostprocessor" ></bean> <bean id= "instantiationawarebeanpostprocessor" class = "cuiyw.spring.service.myinstantiationawarebeanpostprocessor" ></bean> <bean id= "beanfactorypostprocessor" class = "cuiyw.spring.service.mybeanfactorypostprocessor" ></bean> <bean id= "lifecycle" class = "cuiyw.spring.service.lifecycle" init-method= "myinit" destroy-method= "mydestory" > <property name= "name" value= "cuiyw1" ></property> </bean> </beans> |
六、在main中使用bean
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
package cuiyw.springaop; import org.springframework.beans.factory.beanfactory; import org.springframework.context.applicationcontext; import org.springframework.context.support.classpathxmlapplicationcontext; import cuiyw.spring.iservice.iservice; import cuiyw.spring.service.lifecycle; public class app { public static void main( string[] args ) { applicationcontext context= new classpathxmlapplicationcontext( new string[]{ "applicationcontext.xml" }); beanfactory factory=context; lifecycle lifecycle=factory.getbean( "lifecycle" ,lifecycle. class ); lifecycle.setname( "cuiyw2" ); system.out.println( "lifecycle.name=" +lifecycle.getname()); ((classpathxmlapplicationcontext)factory).registershutdownhook(); /*service=(iservice)factory.getbean("servicea"); service.service("cuiyw servicea"); service=(iservice)factory.getbean("serviceimpl"); service.service("cuiyw serviceimpl"); */ } } |
七、輸入打印結果
可以發現輸出的順序和上面圖的順序基本一致。
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。
原文鏈接:http://www.cnblogs.com/5ishare/p/8030038.html