您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)如何在Spring中實(shí)例化bean,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
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"> <!--==============測(cè)試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>
測(cè)試類:
/** * BeanFactory實(shí)例化Bean工程測(cè)試類 */ 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)行的。
關(guān)于如何在Spring中實(shí)例化bean就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。