1. 创建自己的 الفول : beans.xml
<؟
2. 提供解析 XML : : Xmlutils
/** * */package com.cxm.xmlutil ؛ import java.io.inputstream ؛ import java.util.iterator ؛ import java.util.map ؛ import org.jdom.attribute ؛ import org.jdom.document ؛ import ؛ com.cxm.beaninfo.beaninfo ؛/** * Author admin * */public class xmlutils {public static void parsexmldef (inputStream in ، map <string ، beaninfo> beandefmap ، stringbuffer sb) reconsed {saxbuilder reader = reader.build (in) ؛ iterator beanit = xpath.selectnodes (doc ، "/busi-beans/beans/bean"). eiterator() ؛ e ؛ beaninfo beandef ؛ e.getAttribute ("id") ؛ Attribute attrtype = false ؛} beandef.setsingle (issingle) ؛ beandef.setbeanid (attrid.getValue ()) ؛ beandef.settype (attrttype.getValue ()) ؛ beandef.setbeandesc (getText (e ، "desc")) ؛ "Class Olmlass") ؛ // 处理初始化参数 beandefmap.put (attrid.getValue () ، Beandef) ؛}} catch (استثناء e) {e.printstacktrace () ؛}}/** * 根据指定的 element ، 获取 xml 文档内容 * * param p_element * pparam p_xpath p_element ، سلسلة p_xpath) يلقي الاستثناء {string text = null ؛ element e = (element) xPath.SelectSingLenode (p_element ، p_xpath) ؛ if (e! = null) {text = 3. 定义 الفول IO
/** * */package com.cxm.beaninfo ؛/** * Author admin * */public class beaninfo {private String beanid ؛ private string beandesc ؛ public string getBeandesc () setType (نوع السلسلة) {this.type = type ؛} سلسلة خاصة intlclassname ؛ سلسلة عامة getBeanid () {return beanid ؛} public void setBeanid (String beanid) {this.beanid = beanid ؛ inperClassName) {this.implclassname = inslclassname ؛} public boolean issingle () {return issingle ؛} public void setsingle (boolean issingle) {this.issingle = issingle ؛} private boolean issingle = reual ؛} 4.Bean : Beanutil
/** * */package com.cxm.bean ؛/** * Author admin * */public class beanutil {private static xmlbeanfactory factory = new xmlbeanfactory () Factory.CreateBean (p_beanid) ؛}} /** * * /package com.cxm.bean ؛ import java.io.filenotfoundException ؛ import java.io.inputstream com.cxm.beaninfo.beaninfo ؛ import com.cxm.Exception.NosuchbeanDefinitionException ؛ استيراد com.cxm.xmlutil.xmlutils ؛/** * @adminshor admin */public class xmlbeanfactory {private static string bean_xml = "/beans.xml dep < beandefmap = new hashmap <string ، beaninfo> () ؛ خريطة ثابتة خاصة <سلسلة ، كائن> instancemap = new hashmap <string ، object> () ؛ static {inputStream in = xmlbeanfactory.class.getResourCeasstream (bean_xml) ؛ if (in == null) (FiLenOtFoundException e) {e.printstacktrace () ؛}} stringbuffer sb = new StringBuffer () ؛ جرب {xmlutils.parsexmldf (في ، beandefmap ، sb) ؛} catch (استثناء e) beanid.trim () == "") {رمي استثناء جديد ("لا يمكن أن لاغية أو '") ؛} beaninfo beaninfo = beandefmap.get (beanid) ؛ if (null == beanInfo) = instancemap.get (beanid) ؛ if (null! = مثيل) {return مثيل ؛}} سلسلة inslclass = beaninfo.getimplclassname () ؛ مُنشئ <؟ constructor.newinstance () ؛ if (beaninfo.issingle ()) {instancemap.put (beanid ، مثيل) ؛} مثيل الإرجاع ؛}} 5. : :
/** * */package com.cxm.test ؛/** * Author admin * */public interface syshello {void syshello () ؛ World! (syshello) beanutil.createbean ("syshelloimpl") ؛ s.syshello () ؛}}以上这篇 Java 实现 Spring 在 xml 配置 java 类的方法就是小编分享给大家的全部内容了 , 希望能给大家一个参考 , 也希望大家多多支持武林网。