
版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、java模擬spring ioc?xml version= 1.0 eoding= utf-8 ? beans xms= /schema/beans xmlns:xsi= /2001/xmlschema-instance xsi:schemaloion= /schema/beans /schema/beans/spring-beans-2.5.xsd bean = persondao >
2、; bean id= personservice > property name= id value= 24 / property name= name value= zyj / !- property name= persondao ref= persondao / - /bean /beans package junit.test12; import java.util.arraylist; import java.util.list; /* * xml中的 bean/ 的定義 * author adminisator * */ public class beandefinition
3、 private string id; private string clazz; private list propertydefinition propertydefinitions=new arraylist propertydefinition public beandefinition(string id, string clazz) per(); this.id = id; this.clazz = clazz; public string getid() return id; public void id(string id) this.id = id; public strin
4、g getclazz() return clazz; public void setclazz(string clazz) this.clazz = clazz; public list propertydefinition getpropertydefinitions() return propertydefinitions; public void setpropertydefinitions(list propertydefinition propertydefinitions) pertydefinitions = propertydefinitions; nbsp;
5、package junit.test12; public class propertydefinition private string name; private string ref; private string value; public propertydefinition(string name, string ref,string value) super(); = name; this.ref = ref; this.value=value; public string getname() return name; public void setname(s
6、tring name) = name; public string getref() return ref; public void setref(string ref) this.ref = ref; public string getvalue() return value; public void setvalue(string value) this.value = value; package junit.test12; import java.lang.annotation.elementtype; import java.lang.annotation.ret
7、ention; import java.lang.annotation.retentionpolicy; import java.lang.annotation.target; retention(retentionpolicy.runtime) target(elementtype.field,elementtype.method) public interface userdefinresource public string name() default /用法dom4j讀取spring配置文件 package junit.test12; import java.beans.intros
8、pectionexception; import java.beans.introspector; import java.beans.propertydescriptor; import java.lang.reflect.field; import java.lang.reflect.invocationtargetexception; import java.lang.reflect.method; import .url; import java.util.arraylist; import java.util.hashmap; import java.util.list; impor
9、t java.util.map; import junit.test12.beandefinition; import mons.beanuti.convertutils; import mons.beanutils.converter; import org.dom4j.document; import org.dom4j.documentexception; import org.dom4j.element; import org.dom4j.xpath; import org.dom4j.io.saxreader; /* * 自定義容器 * author administrator *
10、*/ public class userdefinedclasspathxmlapplicationcontt private list beandefinition beandefinitions=new arraylist beandefinition private map string, object sigletons=new hashmap string, object public userdefinedclasspathxmlapplicationcontext(string name) this.readxml(filename); this.instancebeans();
11、 this.annotationinject(); this.injectobject(); /* * 讀取xml配置文件 * param filename */ private void readxml(string filename) saxreader saxreader=new saxreader(); document document=null; try url xmlpath=this.getclass().getclassloader().getresource(filename); document=saxreader.read(xmlpath); xpath xpath=d
12、ocument.createxpath( /ns:beans/ns:bean /創(chuàng)建beans/bean查詢(xún)路徑。從根路徑開(kāi)頭 map string, string nsmap=new hashmap string, string nsmap.put( ns , /schema/beans /加入命名空間 xpath.setnamespaceuris(nsmap);/設(shè)置命名空間 list element beans=xpath.selectnodes(document);/獵取文檔下全部bean節(jié)點(diǎn) for (element elem
13、ent : beans) string id=element.attributevalue( id string clazz=element.attributevalue( class beandefinition beandefinition=new beandefinition(id, clazz); xpath xpath2=element.createxpath( ns:property /從相對(duì)路徑開(kāi)頭 xpath2.setnamespaceuris(nsmap); list element propertys=xpath2.selectnodes(element); for (el
14、ement element2 : propertys) string name=element2.attributevalue( name string ref=element2.attributevalue( ref string value=element2.attributevalue( value propertydefinition propertydefinition=new propertydefinition(name, ref,value); beandefinition.getpropertydefinitions().a(propertydefinition); bean
15、definitions.add(beandefinition); catch (documentexception e) / todo auto-generated catch block e.printstacktrace(); /* * 完成bean的實(shí)例化 */ private void instancebeans() try for (beandefinition beandefinition : beandefinitions) if (beandefinition.getclazz()!=null ! .equals(beandefinition.getclazz().trim()
16、 sigletons.put(beandefinition.getid(), class.forname(beandefinition.getclazz().newinstance(); catch (classnotfoundexception e) / todo auto-generated catch block e.printstacktrace(); catch (instantiationexception e) / todo auto-generated catch block e.printstacktrace(); catch (illegalaccessexception
17、e) / todo auto-generated catch block e.printstacktrace(); /* * 為bean對(duì)象的屬性注入值 * throws introspectionexception * throws invocationtargetexception * throws illegalaccessexception * throws illegalargumentexception */ private void injectobject() try for (beandefinition beandefinition : beandefinitions) o
18、bject bean=sigletons.get(beandefinition.getid(); if (bean!=null) propertydescriptor ps=introspector.getbeaninfo(bean.getclass().getpropertydescriptors(); for(propertydefinition propertydefinition:beandefinition.getpropertydefinitions() for (propertydescriptor propertydescriptor : ps) if (propertydef
19、inition.getname().equals(propertydescriptor.getname() method settermethod=propertydescriptor.getwritemethod();/獵取屬性的setter辦法 if (settermethod!=null) object temp=null; if (propertydefinition.getref()!=null ! .equals(propertydefinition.getref().trim() temp=sigletons.get(propertydefinition.getref(); el
20、se if (propertydefinition.getvalue()!=null ! .equals(propertydefinition.getvalue().trim() temp=convertutils.convert(propertydefinition.getvalue(), propertydescriptor.getpropertytype(); settermethod.setaccessible(true);/防止setter辦法為private settermethod.invoke(bean, temp);/把引用對(duì)象注入到屬性 break; catch (exce
21、ption e) / todo auto-generated catch block e.printstacktrace(); /* * 通過(guò)注解實(shí)現(xiàn)注入依靠對(duì)象 * throws introspectionexception * throws invocationtargetexception * throws illegalaccessexception * throws illegalargumentexception */ private void annotationinject() try for (string beanname : sigletons.keyset() obje
22、ct bean=sigletons.get(beanname); if (bean!=null) propertydescriptor ps=introspector.getbeaninfo(bean.getclass().getpropertydescriptors(); for (propertydescriptor propertydescriptor : ps) method settermethod=propertydescriptor.getwritemethod(); if (settermethod!=null settermethod.isannotationpresent(
23、userdefinedresource.class) userdefinedresource userdefinedresource=settermethod.getannotation(userdefinedresource.class); object temp = null; if(userdefinedr()!=null ! .equals(userdefinedr() /一旦指定了name屬性,就只能按名稱(chēng)裝配了 temp = sigletons.get(userdefinedr(); else temp = s
24、igletons.get(propertydescriptor.getname(); if(temp=null) for(string key : sigletons.keyset() if(propertydescriptor.getpropertytype().isassignablefrom(sigletons.get(key).getclass() temp = sigletons.get(key); break; settermethod.setaccessible(true); settermethod.invoke(bean, temp);/把引用對(duì)象注入到屬性 field fi
25、elds = bean.getclass().getdeclaredfields(); for(field field : fields) if(field.isannotationpresent(userdefinedresource.class) userdefinedresource userdefinedresource = field.getannotation(userdefinedresource.class); object temp = null; if(userdefinedr()!=null ! .equals(userdefinedr() temp = sigletons.get(userdefinedr(); else temp = sigletons.get(field.getname(); if(temp=null) for(string key
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 課題代寫(xiě)申報(bào)書(shū)是什么
- 課題申報(bào)評(píng)審書(shū)范例范文
- 就業(yè)心理 課題申報(bào)書(shū)
- 河南小學(xué)課題申報(bào)書(shū)范例
- 兌換房子合同范本
- 公司外匯借款合同范本
- 益智課堂課題研究申報(bào)書(shū)
- 閱讀推廣 課題申報(bào)書(shū)
- 課題申報(bào)項(xiàng)目書(shū)推廣價(jià)值
- 同城工程勞務(wù)合同范例
- 教師師德師風(fēng)考核細(xì)則
- 聲帶腫物的護(hù)理教學(xué)查房
- 2023年高考真題-地理(天津卷) 含答案
- 辦公場(chǎng)所修繕施工方案
- 2025年公務(wù)員考試《行測(cè)》模擬題及答案(詳細(xì)解析)
- 2024年黑龍江省牡丹江市中考?xì)v史試卷
- 2024員工質(zhì)量意識(shí)培訓(xùn)
- 高速公路日常清掃與養(yǎng)護(hù)方案
- 風(fēng)電epc合同模板
- 2024年新人教版一年級(jí)數(shù)學(xué)下冊(cè)《第2單元第5課時(shí) 20以?xún)?nèi)的退位減法解決問(wèn)題(1)》教學(xué)課件
- 2022年陜西省普通高校職業(yè)教育單獨(dú)招生統(tǒng)一考試語(yǔ)文甲(A)試題
評(píng)論
0/150
提交評(píng)論