Spring實(shí)例化bean過程解析及完整代碼示例
提出問題
Spring中Bean的實(shí)例化是Bean生命周期的一個(gè)重要環(huán)節(jié),通常Bean初始化后將不再改變。
那么Spring實(shí)例Bean的過程到底是怎么樣的呢?!
Spring實(shí)例化bean過程分析
要想獲取到一個(gè)bean對(duì)象,得先通過BeanFactory的getBean()方法獲取,期間會(huì)經(jīng)過一系列步驟來實(shí)例化這個(gè)bean對(duì)象:
第一步:調(diào)用Bean的默認(rèn)構(gòu)造方法(當(dāng)然也可以是指定的其它構(gòu)造方法),生成bean實(shí)例:bean1。
第二步:檢查Bean配置文件中是否注入了Bean的屬性值,如果有注入,則在bean1實(shí)例的基礎(chǔ)上對(duì)其屬性進(jìn)行注入,把原來的bean1給覆蓋掉形成新的bean實(shí)例:bean2。
第三步:檢查Bean是否實(shí)現(xiàn)了InitializingBean接口,如果實(shí)現(xiàn)了此接口,則調(diào)用afterPropertiesSet()方法對(duì)bean2進(jìn)行相應(yīng)操作后,把bean2覆蓋形成新的bean實(shí)例:bean3。
第四步:檢查Bean配置文件中是否指定了init-method此屬性,如果已指定,則調(diào)用此屬性對(duì)應(yīng)方法并對(duì)bean3進(jìn)行相應(yīng)操作后,最終把bean3覆蓋形成新的實(shí)例:bean4。
通過上面的步驟我們發(fā)現(xiàn),Spring實(shí)例一個(gè)bean時(shí),這個(gè)bean是在不斷的變化的!
Spring實(shí)例化bean過程代碼演示
為了更好的說明以上步驟,請(qǐng)看下面代碼:
實(shí)體類:
/** * 實(shí)體類 */ public class Employee implements InitializingBean, DisposableBean, BeanNameAware { private String id; // 員工編號(hào) private String name; // 員工姓名 private String sex; // 員工性別 private String age; // 員工年齡 private String nativePlace; // 員工籍貫 private String department; // 員工部門 private String beanName; // bean的名稱 public Employee() { System.out.println("**********第一步:調(diào)用Bean的默認(rèn)構(gòu)造方法**********"); 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是否實(shí)現(xiàn)了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("**********服務(wù)停止**********"); } public void setBeanName(String arg0) { System.out.println("**********設(shè)置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 + "]"; } }
工具類:
/** * Bean上下文工具類 */ public class BeanContextHelper { private static ApplicationContext _instance; static { if (_instance == null) _instance = buildApplicationContext(); } private BeanContextHelper() { } /** * 重新構(gòu)建ApplicationContext對(duì)象 */ public static ApplicationContext buildApplicationContext() { return new ClassPathXmlApplicationContext("applicationContext-base.xml"); } /** * 獲取一個(gè)ApplicationContext對(duì)象 */ public static ApplicationContext getApplicationContext() { return _instance; } }
Spring的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" 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實(shí)例化Bean的過程--> <bean id="employee" class="bean_factory_test.Employee" init-method="init" destroy-method="destroy"> <!--默認(rèn)部門為研發(fā)部的--> <property name="department"> <value>bean2:研發(fā)部</value> </property> </bean> </beans>
測試類:
/** * BeanFactory實(shí)例化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實(shí)例**********"); System.out.println("最終bean的值:" + employee); } }
運(yùn)行結(jié)果:
**********第一步:調(diào)用Bean的默認(rèn)構(gòu)造方法********** bean1的 值:Employee [id=bean1:G080405214, name=null, sex=null, age=null, nativePlace=null, department=null, beanName=null] **********第二步:檢查Bean配置文件中是否注入了Bean的屬性值********** **********設(shè)置bean的名稱********** bean2的值:Employee [id=bean1:G080405214, name=null, sex=null, age=null, nativePlace=null, department=bean2:研發(fā)部, beanName=myBeanName] **********第三步:檢查Bean是否實(shí)現(xiàn)了InitializingBean接口********** bean3的值:Employee [id=bean1:G080405214, name=bean3:李曉紅, sex=bean3:女, age=bean3:25, nativePlace=null, department=bean2:研發(fā)部, beanName=myBeanName] **********第四步:檢查Bean配置文件中是否指定了init-method此屬性********** bean4的值:Employee [id=bean1:G080405214, name=bean3:李曉紅, sex=bean3:女, age=bean3:25, nativePlace=bean3:北京, department=bean2:研發(fā)部, beanName=myBeanName] **********從Spring BeanFactory獲取到的最終bean實(shí)例********** 最終bean的值:Employee [id=bean1:G080405214, name=bean3:李曉紅, sex=bean3:女, age=bean3:25, nativePlace=bean3:北京, department=bean2:研發(fā)部, beanName=myBeanName]
從運(yùn)行結(jié)果看,我們應(yīng)該很清楚Bean實(shí)例化的具體過程了。
Employee實(shí)現(xiàn)了3個(gè)接口:
InitializingBean:此接口提供afterPropertiesSet()方法,它的作用是為bean提供了定義初始化的功能。
DisposableBean:此接口提供destroy()方法,它的作用是在bean實(shí)例銷毀前提供操作的功能。
BeanNameAware:此接口提供setBeanName()方法,它的作用是提供設(shè)置bean名稱的功能,從上面的運(yùn)行結(jié)果可以看出,此方法是在第二步進(jìn)行的。
總結(jié)
以上就是本文關(guān)于Spring實(shí)例化bean的過程的全部內(nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:
《Spring實(shí)現(xiàn)Aware接口自定義獲取bean的兩種方式》
《Java之Spring注解配置bean實(shí)例代碼解析》
如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
相關(guān)文章
Java實(shí)現(xiàn)快速排序和堆排序的示例代碼
這篇文章主要為大家詳細(xì)介紹了快速排序和堆排序的多種語言的實(shí)現(xiàn)(JavaScript、Python、Go語言、Java、C++),感興趣的小伙伴可以了解一下2022-12-12idea設(shè)置JVM運(yùn)行參數(shù)的幾種方式
對(duì)JVM運(yùn)行參數(shù)進(jìn)行修改是JVM性能調(diào)優(yōu)的重要手段,本文主要介紹了idea設(shè)置JVM運(yùn)行參數(shù)的幾種方式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04測試springboot項(xiàng)目出現(xiàn)Test Ignored的解決
這篇文章主要介紹了測試springboot項(xiàng)目出現(xiàn)Test Ignored的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11Mybatis動(dòng)態(tài)SQL之where標(biāo)簽用法說明
這篇文章主要介紹了Mybatis動(dòng)態(tài)SQL之where標(biāo)簽用法說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06Springmvc基于fastjson實(shí)現(xiàn)導(dǎo)包及配置文件
這篇文章主要介紹了Springmvc基于fastjson實(shí)現(xiàn)導(dǎo)包及配置文件,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10Spring入門配置和DL依賴注入實(shí)現(xiàn)圖解
這篇文章主要介紹了Spring入門配置和DL依賴注入實(shí)現(xiàn)圖解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10關(guān)于Spring配置文件加載方式變化引發(fā)的異常詳解
這篇文章主要給大家介紹了關(guān)于Spring配置文件加載方式變化引發(fā)的異常的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Spring具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-01-01