1。
<?xml version = "1.0" encoding = "utf-8"?> <busi-beans> <beans> <bean id = "syshelloimpl" type = "com.cxm.test.syshello"> <desc> test </desc> <impl-crass> com.cxm.test.syshelloimpl </bean> </bean
2。xml类:xmlutils
/** * */パッケージcom.cxm.xmlutil; Import java.io.inputStream; Import java.util.iterator; Import java.util.map; Import org.jdom.attribute; import org.jdom.document; import org.jdom.element; Import org.jdom.daxbuilder.daxbuilder.daxbuilder.daxbuilder.daxbuilder.daxbuilder.daxbuilder.daxbuilder.daxbuilder.daxbuilder.daxbuilder. com.cxm.beaninfo.beaninfo;/** * @author admin * */public class xmlutils {public static void parsexmldef(inputstream in、map <string、beaninfo> beandefmap、stringbuffer sb)スロー例外reader.build(in); iterator beanit = xpath.selectnodes(doc、 "/busi-beans/beans/bean").iterator(); element e; beaninfo beandef; e.getAttribute( "id"); attribute atttype = e.getAttribute( "type"); attibute singletype = e.getattribute( "single"); boolean sissingle = true; if(null!= singletype && "1" .equals(singletype.getvalue()){singingle = {sissingLe false;} beandef.setsingle(issingle); beandef.setbeanid(attrid.getValue()); beandef.settype(atttype.getValue()); beandef.setbeandesc(gettext(e、 "desc")); "Impl-class")); // p_element、string p_xpath)スロー例外{string text = null; element e =(element)xpath.selectsinglenode(p_element、p_xpath); if(e!= null){text = e.getText();} else {} return text;}}}} 3. bean io
/*** * */パッケージcom.cxm.beaninfo;/** * @author admin * */public class beaninfo {private string beanid; private string type; private string beandesc; public beandesc(){return beandesc;} public void setbeandesc(this beandesc = beandesc = beandesc = beandesc = beandesc = beandesc = beandesc = beandesc = beandesc getType(){return type;} public void setType(string type){this.type = type;} private string entlclassname; public string getbeanid(){return beanid;} public void setbeanid(string beanid){this.beanid = beanid;} public string getimplclclsname;} SetimplClassName(String ImplClassName){this.implClassName = ImplClassName;} public Boolean Issingle(){return Issingle;} public void setSingle(boolean issingle){this.issingle = issingle;} private boolean issingle = true;} 4.ビーン的创建类:Beanutil
/** * */パッケージcom.cxm.bean;/** * @author admin * */public class beanutil {private xmlbeanfactory Factory = new XmlbeanFactory();/** *获取定义好的Bean Factory.createbean(p_beanid);}} /** * * /package com.cxm.bean; import java.io.fileenotfoundexception; Import java.io.inputstream; import java.lang.reflect.constuctor; Import Java.util.util.util.Map; Import Java.Util.Map; com.cxm.beaninfo.beaninfo; Import com.cxm.exception.nosuchbeandefinitionexception;インポートcom.cxm.xmlutils;/** * @author admin * */public class xmlbeanfactory {private static string bean_xml = "/bbeans.xml purtingiate beandefmap = new Hashmap <String、Beaninfo>(); private static Map <string、object> instancemap = new Hashmap = new Hashmap = new Hashmap = new Hashmap = new Hashmap = object>(); static {inputstream in = xmlbeanfactory.class.getResourceasstream( (filenotfoundexception e){e.printstacktrace();}} stringbuffer sb = new stringbuffer(); try {xmlutils.parsexmldef(in、beandefmap、sb);} catch(exception e){throw new runtimeexception();}}パブリックオブジェクトCreate createbuean(String bean()例外{if(beanid == null || beanid.trim()== ""){throw new Exception( "beanidはnullまたは ''");} beaninfo beaninfo = beandefmap.get(beanid); if(null == beaninfo){新しいnosuchbeandefinitionexception(define in xml ";" instance; if(beaninfo.issingle()){instance = instancemap.get(beanid); if(null!= instance){return instance;}} string entlclass = beaninfo.getimplclassname(); constructor <?> construction = class.forname() constructor.newinstance(); if(beaninfo.issingle()){instancemap.put(beanid、instance);} return instance;}}} 5。
/** * */パッケージcom.cxm.test;/** * @author admin * */public interface syshello {void syshello();}/** * */package com.cxm.test;/** * @author admin * */public class syshelloimpl実装syshello world! ");}}/** * */package com.cxm.test; import com.cxm.bean.beanutil;/** * @author admin * */public class test {/** * @param args */public static void main(string [] args)Throws Exception (syshello)beanutil.createbean( "syshelloimpl"); s.syshello();}}}以上这篇java实现春xml配置java类的方法就是小编分享给大家的全部内容了、希望能给大家一个参考、也希望大家多多支持武林网。