1. 创建自己的 Bean 文件 : Beans.xml
<?xml version="1.0" encoding="UTF-8"?><busi-beans><beans><bean id="SysHelloImpl" type="com.cxm.test.SysHello"><desc>test</desc><impl-class>com.cxm.test.SysHelloImpl</impl-class></bean></beans></busi-beans>
2. 提供解析 xml 类 : xmlutils
/** * */pacote com.cxm.xmlutil; importar java.io.inputStream; importar java.util.iterator; importar java.util.map; importar org.jdom.attribute; importrg.jdom.document; import org.jorder.element; import.jdom.input.jdom.jdom.document; import org.jdomement; import.jdom.input.input.jdom.jdom.document; import org.jorder; com.cxm.beaninfo.beaninfo;/** * @author admin * */public class xmlutils {public static void parsexmldEf (inputStream in, map <string, beaninfo> beandefmap, stringbuffer sb) (saxbuilder ssester = string sbuffer) reader.build (in); iterator beanit = xpath.selectNodes (doc, "/busi-beans/beans/bean" ).iterator(); elemento e; beaninfo beandf; while (bEanit.hasnext ()) {beandex = New Beaninfo (); e.getAttribute ("id"); attribute attType = e.getAttribute ("type"); attribute singleType = e.getAttribute ("single"); boolean Issingle = true; if (null! false;} beandef.Setsingle (Issingle); beandef.setBeanid (attid.getValue ()); beandef.setType (atttype.getValue ()); beandef.setBeandSc (getText (e, "desct"); "Impl-Class")); // 处理初始化参数 beandefmap.put (attid.getValue (), beandef);}} catch (Exceção e) {e.printStacktrace ();}}/** ** 根据指定的 elemento, xpath 获取 xml 文档内容 * * @param p_element * @param p_param p_xpath * p_element, string p_xpath) lança exceção {string text = null; elemento e = (elemento) xpath.selectsinglenode (p_element, p_xpath); if (e! = null) {text = e.getText ();} else {} retornar texto;}}} 3. 定义 Bean io
/** * */pacote com.cxm.beaninfo;/** * @author admin * */public class beaninfo {private string beanid; private string type; private string beandesc; public string getBeandesc () {return beandesc;} public void SetBeNDesc (string beanDesc) { type;} public void setType (string type) {this.type = type;} private string ImplClassName; public String getBeanid () {return beanid;} public void setBeanid (string beanid) {this.Beanid = beanid;} public string getImplassName () {retorn a implorar ImplClassName) {this.implClassName = ImplClassName;} public boolean Issingle () {return Issingle;} public void Setsingle (boolean Issingle) {this.issingle = Issingle;} Private Boolean Issingle = true;} 4.Bean 的创建类 : Beanutil
/** * */pacote com.cxm.bean;/** * @author admin * */public class beanutil {private estático xmlBeanFactory Factory = new XmlBeanFactory ();/** ** * 获取定义好的 Bean 对象 * @param p_beanid * @return * @Therows Exception */static ObjectBeBeBean (public * Factory.CreateBean (P_Beanid);}} /** * * /pacote com.cxm.bean; importar java.io.filenotfoundException; importar java.io.inputStream; importação java.lang.reflect.constructor; import java.util.hashmap; com.cxm.beaninfo.beanInfo; importar com.cxm.exception.nosuchbeandEfinitionException; import com.cxm.xmlutil.xmlutils;/** * @author admin * */public class xmlBeanFactory {private static string bean_xml = bumbs.x/ beandefmap = new hashmap <string, beaninfo> (); mapa estático privado <string, objeto> instancemap = new hashmap <string, object> (); static {inputStream in = xmlBeanFactory.class.GeRSourSstream (bean_xml); se (em === 9) (FileNotfoundException e) {e.printStackTrace ();}} stringbuffer sb = new StringBuffer (); tente {xmlutils.parsexmldEf (in, beandefmap, sb);} catch (excepção e) {throw runtimeException ();}}}}} || beanid.Trim () == "") {tire nova exceção ("Beanid não pode nulo ou ''");} beaninfo beaninfo = beandefmap.get (beanid); if (null == beanInfo) {tiro novo nosuchbendEnitionException ("beanId é instance; if (beaninfo.issingle ()) {instance = instancemap.get (beanid); if (null! = instance) {return instance;}} string implclass = beaninfo.getImplClassName (); construtor <? construtor.newInstance (); if (beaninfo.issingle ()) {Instancemap.put (beanid, instância);} retornar a instância;}} 5. 测试:
/** * */pacote com.cxm.test;/** * @author admin * */interface pública syshello {void syshello ();}/** * */package com.cxm.test;/** * @author admin * * public class mundo! ");}}/** * */pacote com.cxm.test; importar com.cxm.bean.beanutil;/** * @author admin * */public class Test {/** * @param args * @throws Exception */public static void main (string [] args) Throws Exception {Syshello (Syshello) beanutil.createbean ("syshelloimpl"); s.shello ();}}以上这篇 java 实现 mola 在 xml 配置 java 类的方法就是小编分享给大家的全部内容了 , 希望能给大家一个参考 , 也希望大家多多支持武林网。