1. 创建自己的ถั่ว文件: Beans.xml
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <busi-beans> <beans> <bean id = "syshelloimpl" type = "com.cxm.test.yshello"> <esc> test </desc>
2. 提供解析 xml 类: xmlutils
/** * */แพ็คเกจ com.cxm.xmlutil; นำเข้า java.io.inputstream; นำเข้า java.util.iterator; นำเข้า java.util.map; นำเข้า org.jdom.attribute; นำเข้า org.jdom.document; นำเข้า org.jdom.element; com.cxm.beaninfo.beaninfo;/** * @author admin * */คลาสสาธารณะ xmlutils {โมฆะสาธารณะคงที่ parsexmldef (inputstream in, แผนที่ <string, beaninfo> beandefmap, Stringbuffer sb) reader.build (in); iterator beanit = xpath.selectnodes (หมอ, "/busi-beans/beans/Bean") TITERATOR(); ELEMENT E; beaninfo beandef; ในขณะที่ (beanit.hasnext ()) e.getAttribute ("id"); แอตทริบิวต์ attRtype = e.getAttribute ("type"); แอตทริบิวต์ singleType = e.getAttribute ("single"); boolean issingle = true; if (null! = singleType && "1" .equals false;} beandef.setsingle (ออก); beandef.setBeanid (attrid.getValue ()); beandef.setType (attrtype.getValue ()); beandef.setBeandesc (getText (e, "desc")); "impl-class")); // 处理初始化参数 beandefmap.put (attrid.getValue (), beandef);}} catch (ข้อยกเว้น e) {e.printstacktrace ();}}/** * 根据指定的 @ @ @ @ @ @ @ @ @parturn @@paramtint p_element, สตริง p_xpath) พ่นข้อยกเว้น {สตริงข้อความ = null; องค์ประกอบ e = (องค์ประกอบ) xpath.selectsingLenode (p_element, p_xpath); ถ้า (e! = null) {text = e.getText (); 3. 定义ถั่ว io
/** * */แพ็คเกจ com.cxm.beaninfo;/** * @author admin * */คลาสสาธารณะ beaninfo {สตริงส่วนตัว beanid; ประเภทสตริงส่วนตัว; สตริงส่วนตัว beandesc; สตริงสาธารณะ getebeandesc () {กลับ beandesc;} สาธารณะ type;} โมฆะสาธารณะ setType (ประเภทสตริง) {this.type = type;} สตริงส่วนตัว interclassName; สตริงสาธารณะ getBeanid () {return beanid;} โมฆะสาธารณะ setBeanId (สตริง beanid) {this.beanid = beanid; interclassName) {this.implClassName = ImlclassName;} บูลีนสาธารณะที่ออก () {return issingle;} โมฆะสาธารณะ setsingle (บูลีนออก) {this.issingle = issingle;} บูลีนส่วนตัว 4.Bean 的创建类: Beanutil
/** * */แพ็คเกจ com.cxm.bean;/** * @author admin * */คลาสสาธารณะ beanutil {private static xmlbeanfactory โรงงาน = ใหม่ xmlbeanfactory ();/** * 获取定义好的ถั่ว对象 * @param p_beanid * @return * @throws Factory.CreateBean (p_beanid);}} /** * * /แพ็คเกจ com.cxm.bean; นำเข้า java.io.filenotfoundexception; นำเข้า java.io.inputstream; นำเข้า java.lang.reflect.constructor; com.cxm.beaninfo.beaninfo; นำเข้า com.cxm.exception.nosuchbeandefinitionexception; นำเข้า com.cxm.xmlutil.xmlutils;/** * @author admin * */ชั้นเรียนสาธารณะ beanDefMap = new hashmap <string, beaninfo> (); แผนที่คงที่ส่วนตัว <สตริง, วัตถุ> instancemap = new hashmap <string, object> (); คงที่ {inputstream ใน = xmlbeanfactory.class.getResourceasstream (bean_xml); (FilenotFoundException E) {E.printStackTrace ();}} StringBuffer sb = new StringBuffer (); ลอง {xmlutils.parsexmldef (ใน, beandefmap, sb);} catch (ยกเว้น e) ข้อยกเว้น {ถ้า (beanid == null || beanid.trim () == "") {โยนข้อยกเว้นใหม่ ("beanid ไม่สามารถ null หรือ ''");} beaninfo beaninfo = beandefmap.get (beanid); ถ้า null == beaninfo) อินสแตนซ์; ถ้า (beaninfo.issingle ()) {อินสแตนซ์ = instancemap.get (beanid); ถ้า (null! = อินสแตนซ์) {return instance;}} สตริง interclass = beaninfo.getimplclassName (); constructor < Constructor.NewInstance (); if (beaninfo.issingle ()) {instancemap.put (beanid, อินสแตนซ์);} return อินสแตนซ์;}} 5. 测试:
/** * */แพ็คเกจ com.cxm.test;/** * @author admin * */อินเตอร์เฟสสาธารณะ syshello {void syshello ();}/** * */แพ็คเกจ com.cxm.test;/** * @author admin * */คลาสสาธารณะ syshelloimpl World! ");}}/** * */แพ็คเกจ com.cxm.test; นำเข้า com.cxm.bean.beanutil;/** * @author admin * */การทดสอบคลาสสาธารณะ {/** * @param args * @throws ข้อยกเว้น (syshello) beanutil.createBean ("syshelloimpl"); s.syshello ();}}以上这篇 java 实现ฤดูใบไม้ผลิ在 xml 配置 java 类的方法就是小编分享给大家的全部内容了, 希望能给大家一个参考希望能给大家一个参考也希望大家多多支持武林网。也希望大家多多支持武林网。