1. 创建自己的 Bean 文件 : Beans.xml
<? xml versión = "1.0" encoding = "utf-8"?> <Busi-beans> <Beans> <bean id = "syshelloiMpl" type = "com.cxm.test.syshello"> <Edc> test </desc> <lass> com.cx.test.syshelloiMpl <//implosshass> </bean> </bean> </behremi> </behuss>
2. 提供解析 xml 类 : xmlutils
/** * */paquete com.cxm.xmlutil; import java.io.inputstream; import java.util.iterator; import java.util.map; import org.jdom.attribute; import og.jdom.document; import org.jdom.element; import com.cxm.beaninfo.beaninfo;/** * @author admin * */public class xmlUtils {public static void parsexmldef (inputStream in, map <string, beanfo> beandefmap, stringbuffer sb) lanza excepción {saxbuilder lector = new saxbuilder (false); document doc = nleull; prueba {prueba; Reader.build (in); iterator beanit = xpath.selectnodes (doc, "/busi-beans/beans/bean").iterator() ;element e; beaninfo beandef; while (beanit.hasnext ()) {beandef = new beaninfo (); e = (elemento) E.getAttribute ("id"); atribute attrType = e.getAttribute ("type"); attribute singletype = e.getAttribute ("sencillo"); boolean issingle = true; if (null! = singletype && "1" .equals (singletype.getValue ())) {issInLing falso;} beandef.setsingle (issingle); beandef.setBeanId (attrid.getValue ()); beandef.settype (attrType.getValue ()); beandef.setBeanDesc (getText (e, "desc")); beandef.setImplSclassName (gettext (e, e, e, "Impl-class")); // 处理初始化参数 beandefmap.put (attrid.getValue (), beandef);}} catch (excepción e) {e.pintStackTrace ();}}/** * 根据指定的 elemento, xpath 获取 xml 文档内容 * * @param p_emement * @param p_xpath * @return * 文档内容 文档内容 * * @param p_emement * @param p_xpath * @return * 文档内容 文档内容 文档内容 * * @param p_emement * @param p_xpath * @return * 文档内容 文档内容 文档内容 * * @param p_emement * @param p_xpath * @return * @throws. p_element, string p_xpath) lanza la excepción {string text = null; element e = (element) xpath.selectsinglenode (p_element, p_xpath); if (e! = null) {text = e.getText ();} else {} return text;}}}} 3. 定义 Bean io
/** * */paquete com.cxm.beaninfo;/** * @author admin * */public class BeanInfo {private String string beanId; private string type; private string beandesc; public string getBeAnDesc () {return beanDesc;} public void setBeanDesc (string beanDesc) {this.BeanDesc = beandesc;} public string getType () {) settype (type de cadena) {this.type = type;} private string implClassName; public String getBeanId () {return beanId;} public void setBeanId (string beanId) {this.BeanId = beanId;} public String getImplClassNameNameNclassNameNameName;} public void setImpLassName (String ImplsClassName) implClassName;} public boolean isSingle () {return issingle;} public void setSingle (boolean issingle) {this.issingle = issingle;} privado booleano issingle = true;} 4.Bean 的创建类 : Beanutil
/** * */paquete com.cxm.bean;/** * @author admin * */public class BeanUtil {private static xmlBeanFactory factory = new xmlBeanFactory ();/** * 获取定义好的 bean 对象 * @param p_beanid * @return * @throws excepcion */public static create aBean (String P_Beanid) factory.createBean (p_beanid);}} /** * * /paquete 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; import; importar; importar; import com.cxm.beaninfo.beaninfo; import com.cxm.exception.nosuchbeanDefinitionException; import com.cxm.xmlutil.xmlutils;/** * @author admin * */public class xmlBeanFactory {string static static bean_xml = "/"/beans.xml"; beandefmap = new Hashmap <String, BeanInfo> (); private Static Map <String, Object> InstanceMap = new HashMap <String, Object> (); Static {inputStream in = xMlBeanFactory.Class.Class.getResourceasStream (bean_xml); if (in == null) (FileNotFoundException e){e.printStackTrace();}}StringBuffer sb = new StringBuffer();try{XmlUtils.parseXmlDef(in, beanDefMap, sb);}catch (Exception e){throw new RuntimeException();}}public Object createBean(String beanId) throws Exception{if(beanId==null || beanId.trim () == "") {Throw New Exception ("BeanId no puede nulo o '' '");} beaninfo beanInfo = beandefmap.get (beanId); if (null == beanInfo) {tire new NoSuchBeanDefinitionException ("BeanId no se define en XML");} OBJETIS = instancEMap.get (beanId); if (null! = instancia) {return instance;}} string implClass = beanInfo.getImplClassName (); constructor <?> constructor = class.forname (implClass.trim ()). getConductor (); instancia = instancia = constructor.newinStance (); if (beanInfo.issingle ()) {instancEMap.put (beanId, instancia);} return instancia;}} 5. 测试 :
/** * */paquete com.cxm.test;/** * @author admin * */public interface syshello {void syshello ();}/** * */paquete com.cxm.test;/** * @author administrador * */public class syshelloMpl implementa syshello {@Overridepublic void syshello () () World! ");}}/** * */paquete com.cxm.test; import com.cxm.bean.beanutil;/** * @author admin * */public class test {/** * @param args * @throws excepción */public static void main (string [] args) Excepción {syshello s = s = (Syshello) Beanutil.CreateBean ("Syshelloimpl"); S.Syshello ();}}以上这篇 java 实现 spring 在 xml 配置 java 类的方法就是小编分享给大家的全部内容了 , 希望能给大家一个参考 也希望大家多多支持武林网。 也希望大家多多支持武林网。