1。创建自己的bean:beans.xml
<?xml版本=“ 1.0”编码=“ utf-8”?> <busi-beans> <beans> <bean id =“ syshelloimpl” type =“ com.cxm.test.syshello”> <desc> </desc> test </desc> test </desc> </desc> <spland-class> <spland-class> com.cxm.c com.cxm.cx.syshelloimpl </bues buean>
2。xml类:xmlutils
/** * */包com.cxm.xmlutil; import java.io.inputStream; import java.util.iterator; import java.util.map; import org.jdom.jdom.attribute; import org.jdom.jdom.jdom.jdom.jdom.document; import org.jdom.jdom.element; elemt org.jdody; eeld empt; com.cxm.beaninfo.BeanInfo;/** * @author admin * */public class XmlUtils{public static void parseXmlDef(InputStream in, Map<String,BeanInfo> beanDefMap,StringBuffer sb) throws Exception{SAXBuilder reader = new SAXBuilder(false);Document doc = null;try{doc = reader.build(in); iterator beanit = xpath.selectNodes(doc,“/busi-beans/beans/beans/bean").iterator(eleterator(); element E; element e; eleminfo beandef; while while(beant.hasnext()) e.getAttribute(“ id”); attibute attrype = e.getAttribute(“ type”); attribute singletype = e.getAttribute(“ single”); boolean issingle = true; if(null!= singletype && &&&&&&&“ 1” .equals(singletype.getValue(issingvalue()) false;} beandef.setsingle(issingle); beandef.setBeanid(attrid.getValue()); beandef.settype(attrype.getValue()); beandef.setBeandesc(getText(getText)(getText(e,e,d desc'd desc'd desc') “ Impl-class”)); //处理初始化参数beandefmap.put(attrid.getValue(),beandef);}}} catch(exudepion e){e.printstacktrace();}}}}}}/** *根据指定的元素,xpath p_element,字符串p_xpath)抛出异常{字符串text = null; element e =(element)xpath.selectsinglenode(p_element,p_xpath); if(e!= null){text = e.getText() 3. bean io
/** * */软件包com.cxm.beaninfo;/** * @author admin * */public class beaninfo {private string beanid; private string; private string beandesc; public string getBeanDesc(){return beandesc;} type;} public void setType(string type){this.type = type;}私有字符串inplclassName; public string getBeanId(){return beanid;} public void setBeanId(string beanid){this.beanid = beanid = beanid;} public string getimplclassname string InplClassName){this.implClassName = inplclassName;} public boolean issingle(){返回issingle;} public void setSingle(boolean issingle){this.issingle = isingle = issingle;}私人布尔;} 4. Bean的创建类:Beanutil
/** * */package com.cxm.bean;/** * @author admin * */public class beanutil {private static xmlbeanfactory fartical = new xmlbeanfactory() factory.createBean(p_beanId);}} /** * */package com.cxm.bean;import java.io.FileNotFoundException;import java.io.InputStream;import java.lang.reflect.Constructor;import java.util.HashMap;import java.util.Map;import com.cxm.beaninfo.beaninfo; import com.cxm.exception.nosuchbeandefinitionException; import com.cxm.cxm.xmlutil.xmlutils;/** * @Author admin */admin */admin */public class class class class class xmlbeanfactory xmlbeanfactory {private static Strate string bean_xml = beandefmap = new Hashmap <字符串,beaninfo>();私有静态映射<string,object> instancemap = new Hashmap <string,object>(); static {inputStream in = xmlbeanfactory.class.class.getResresourceasourceasorceorceastream(bean_xml) (filenotfoundException e){e.printstacktrace();}} stringBuffer sb = new StringBuffer(); try {xmlutils.parsexmldef(in,beandefmap,sb);} || beanId.trim()==""){throw new Exception("BeanId can not NULL or '' ");}BeanInfo beanInfo = beanDefMap.get(beanId);if(null ==beanInfo ){throw new NoSuchBeanDefinitionException(" beanid is not define in xml");}Object instance;if(beanInfo.isSingle()){instance = instancemap.get(beanid); if(null!= instance){return instance;}} string string inplclass = beaninfo.getImplClassName(); structionor <?> structionor <?> classor = class.forname(inplclass.trim() constructor.newinstance(); if(beaninfo.issingle()){instancemap.put(beanid,instance);} return instance;}}}} 5。测试:
/** * */package com.cxm.test;/** * @author admin * */public interface SysHello{void sysHello();} /** * */package com.cxm.test;/** * @author admin * */public class SysHelloImpl implements SysHello{@Overridepublic void sysHello(){System.out.println("hello world!”);}}}/** * */package com.cxm.test; import com.cxm.bean.beanutil;/** * @author Admin * */public class test {/** * @param args * @param args * @throws * @throws * @throws * @throws * @throws excrideion */public static void main(string [string] syshellos syshello s = syshello s = syshello s = syshello s = (syshello)beanutil.createbean(“ syshelloimpl”); s.syshello();}}}以上这篇java spring在xml配置java类的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持武林网。