提出問題
Spring中Bean的實例化是Bean生命周期的一個重要環節,通常Bean初始化后將不再改變。
那么Spring實例Bean的過程到底是怎么樣的呢?!
Spring實例化bean過程分析
要想獲取到一個bean對象,得先通過BeanFactory的getBean()方法獲取,期間會經過一系列步驟來實例化這個bean對象:
第一步:調用Bean的默認構造方法(當然也可以是指定的其它構造方法),生成bean實例:bean1。
第二步:檢查Bean配置文件中是否注入了Bean的屬性值,如果有注入,則在bean1實例的基礎上對其屬性進行注入,把原來的bean1給覆蓋掉形成新的bean實例:bean2。
第三步:檢查Bean是否實現了InitializingBean接口,如果實現了此接口,則調用afterPropertiesSet()方法對bean2進行相應操作后,把bean2覆蓋形成新的bean實例:bean3。
第四步:檢查Bean配置文件中是否指定了init-method此屬性,如果已指定,則調用此屬性對應方法并對bean3進行相應操作后,最終把bean3覆蓋形成新的實例:bean4。
通過上面的步驟我們發現,Spring實例一個bean時,這個bean是在不斷的變化的!
Spring實例化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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
/** * 實體類 */ public class Employee implements InitializingBean, DisposableBean, BeanNameAware { private String id; // 員工編號 private String name; // 員工姓名 private String sex; // 員工性別 private String age; // 員工年齡 private String nativePlace; // 員工籍貫 private String department; // 員工部門 private String beanName; // bean的名稱 public Employee() { System.out.println( "**********第一步:調用Bean的默認構造方法**********" ); this .id = "bean1:G080405214" ; System.out.println( "bean1的 值:" + this ); System.out.println( "**********第二步:檢查Bean配置文件中是否注入了Bean的屬性值**********" ); } public void afterPropertiesSet() throws Exception { System.out.println( "bean2的值:" + this ); System.out.println( "**********第三步:檢查Bean是否實現了InitializingBean接口**********" ); this .name = "bean3:李曉紅" ; this .sex = "bean3:女" ; this .age = "bean3:25" ; System.out.println( "bean3的值:" + this ); } public void init() { System.out .println( "**********第四步:檢查Bean配置文件中是否指定了init-method此屬性**********" ); this .nativePlace = "bean3:北京" ; System.out.println( "bean4的值:" + this ); } public void destroy() throws Exception { System.out.println( "**********服務停止**********" ); } public void setBeanName(String arg0) { System.out.println( "**********設置bean的名稱**********" ); this .beanName = "myBeanName" ; } public String getId() { return id; } public void setId(String id) { this .id = id; } public String getName() { return name; } public void setName(String name) { this .name = name; } public String getSex() { return sex; } public void setSex(String sex) { this .sex = sex; } public String getAge() { return age; } public void setAge(String age) { this .age = age; } public String getNativePlace() { return nativePlace; } public void setNativePlace(String nativePlace) { this .nativePlace = nativePlace; } public String getDepartment() { return department; } public void setDepartment(String department) { this .department = department; } public String getBeanName() { return beanName; } @Override public String toString() { return "Employee [id=" + id + ", name=" + name + ", sex=" + sex + ", age=" + age + ", nativePlace=" + nativePlace + ", department=" + department + ", beanName=" + beanName + "]" ; } } |
工具類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
/** * Bean上下文工具類 */ public class BeanContextHelper { private static ApplicationContext _instance; static { if (_instance == null ) _instance = buildApplicationContext(); } private BeanContextHelper() { } /** * 重新構建ApplicationContext對象 */ public static ApplicationContext buildApplicationContext() { return new ClassPathXmlApplicationContext( "applicationContext-base.xml" ); } /** * 獲取一個ApplicationContext對象 */ public static ApplicationContext getApplicationContext() { return _instance; } } |
Spring的Bean配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<? 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:context = "http://www.springframework.org/schema/context" xmlns:aop = "http://www.springframework.org/schema/aop" xmlns:tx = "http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!--==============測試Spring BeanFactory實例化Bean的過程--> < bean id = "employee" class = "bean_factory_test.Employee" init-method = "init" destroy-method = "destroy" > <!--默認部門為研發部的--> < property name = "department" > < value >bean2:研發部</ value > </ property > </ bean > </ beans > |
測試類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
/** * BeanFactory實例化Bean工程測試類 */ public class Test { public static void main(String args[]) { Test test = new Test(); test.test(); } public void test() { ApplicationContext context = BeanContextHelper.getApplicationContext(); Employee employee = (Employee) context.getBean( "employee" ); System.out.println( "**********從Spring BeanFactory獲取到的最終bean實例**********" ); System.out.println( "最終bean的值:" + employee); } } |
運行結果:
1
2
3
4
5
6
7
8
9
10
11
|
**********第一步:調用Bean的默認構造方法********** bean1的 值:Employee [id=bean1:G080405214, name=null, sex=null, age=null, nativePlace=null, department=null, beanName=null] **********第二步:檢查Bean配置文件中是否注入了Bean的屬性值********** **********設置bean的名稱********** bean2的值:Employee [id=bean1:G080405214, name=null, sex=null, age=null, nativePlace=null, department=bean2:研發部, beanName=myBeanName] **********第三步:檢查Bean是否實現了InitializingBean接口********** bean3的值:Employee [id=bean1:G080405214, name=bean3:李曉紅, sex=bean3:女, age=bean3:25, nativePlace=null, department=bean2:研發部, beanName=myBeanName] **********第四步:檢查Bean配置文件中是否指定了init-method此屬性********** bean4的值:Employee [id=bean1:G080405214, name=bean3:李曉紅, sex=bean3:女, age=bean3:25, nativePlace=bean3:北京, department=bean2:研發部, beanName=myBeanName] **********從Spring BeanFactory獲取到的最終bean實例********** 最終bean的值:Employee [id=bean1:G080405214, name=bean3:李曉紅, sex=bean3:女, age=bean3:25, nativePlace=bean3:北京, department=bean2:研發部, beanName=myBeanName] |
從運行結果看,我們應該很清楚Bean實例化的具體過程了。
Employee實現了3個接口:
InitializingBean:此接口提供afterPropertiesSet()方法,它的作用是為bean提供了定義初始化的功能。
DisposableBean:此接口提供destroy()方法,它的作用是在bean實例銷毀前提供操作的功能。
BeanNameAware:此接口提供setBeanName()方法,它的作用是提供設置bean名稱的功能,從上面的運行結果可以看出,此方法是在第二步進行的。
總結
以上就是本文關于Spring實例化bean的過程的全部內容,希望對大家有所幫助。如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
原文鏈接:https://www.cnblogs.com/leskang/p/6411011.html