版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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 /创建beans/bean查询路径。从根路径开头 map string, string nsmap=new hashmap string, string nsmap.put( ns , /schema/beans /加入命名空间 xpath.setnamespaceuris(nsmap);/设置命名空间 list element beans=xpath.selectnodes(document);/猎取文档下全部bean节点 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 /从相对路径开头 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的实例化 */ 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对象的属性注入值 * 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);/把引用对象注入到属性 break; catch (exce
21、ption e) / todo auto-generated catch block e.printstacktrace(); /* * 通过注解实现注入依靠对象 * 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属性,就只能按名称装配了 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);/把引用对象注入到属性 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. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 电力市场交易合同(2篇)
- 电话客服服务合同(2篇)
- 病理检测服务合同(2篇)
- 2024-2025学年新教材高中物理课时跟踪训练十八共点力的平衡含解析鲁科版必修第一册
- 2024-2025学年高中历史第6单元现代世界的科技与文化第29课百花齐放百家争鸣教案含解析岳麓版必修3
- 销售合同管理excel表格系统台账登记统计到期提醒Excel表
- 政治教师教学工作总结
- 大一学生期末自我总结
- 高二下学期语文教师工作总结
- 村委会妇女主任个人总结
- 三年级上册数学脱式计算大全600题及答案
- 计算机控制系统 课件 第10章 网络化控制系统的分析与设计
- 鲁教版(五四制)七年级数学上册期末考试卷-附带答案
- 南京大学仪器分析习题集
- 空调维保应急预案
- 小学六年级数学上册解决问题专项必考题西师大版
- 2023年高考语文全国乙卷作文范文及导写(解读+素材+范文)课件版
- 模块建房施工方案
- 多域联合作战
- 美容美发场所卫生规范
- 《隧道工程》(第二版)课件 第1、2章 绪论、隧道工程勘测
评论
0/150
提交评论