1. 创建自己的 Bean 文件 : Beans.xml
<? xml version = "1.0" Encoding = "UTF-8"?> <Busi-beans> <adeceman> <bean id = "syshelloimpl" type = "com.cxm.test.syshello"> <cesc> test </desc> <Impl-class> com.cxm.test.sishelloImpl </Implass> </bean> </bean> </bean> </bean> </bean> <//bean> </bean>
2. 提供解析 Xml 类 : Xmlutils
/** * *//пакет com.cxm.xmlutil; import java.io.inputstream; импорт java.util.iterator; import java.util.map; импорт org.jdom.atribute; импорт org.jdom.document; import.jdom.element; импорт org.jdom.saxbuilder; com.cxm.beaninfo.beaninfo;/** * @author admin * */public class xmlutils {public static void parsexmldef (inputstream in, map <string, beaninfo> beandefmap, stringbuffer sb) trops exection {saxbuilder reader = new sabuilder (false); reader.build (in); iterator beanit = xpath.selectnodes (doc, "/busi-beans/beans/bean").terator()? Element e; Beaninfo beandef; while (beanit.hasnext ()) {beandef = new Beaninfo (election). e.getattribute ("id"); attribute attrtype = e.getattribute ("type"); атрибут singletype = e.getattribut false;} beandef.setsingle (issingl); beandef.setbeanid (attrid.getvalue ()); beandef.settype (attrtype.getvalue ()); beandef.setbaindesc (getText (e, "desc")); beandef.setimplclassnam "impl-class"));//处理初始化参数beanDefMap.put(attrId.getValue(), beanDef);}}catch (Exception e){e.printStackTrace();}}/** * 根据指定的element, xPath获取XML文档内容 * * @param p_element * @param p_xPath * @return * @throws Exception */public static String getText(Element p_Element, string p_xpath) throws exection {string text = null; element e = (element) xpath.selectsinglenode (p_element, p_xpath); if (e! = null) {text = e.getText ();} els 3. 定义 Бин io
/** * */package com.cxm.beaninfo;/** * @author admin * */public class beaninfo {private String beanid; Private String Type; Private String Beandesc; public String getBeandesc () {return beandesc;} public void setbaindesc (string beandesc) {this.beandesc = beandesc;} publiceSc (restresse) {this.beandesc =} publiceSec; type;} public void settype (string type) {this.type = type;} private string inmplassname; public String getBeanid () {return beanid;} public void setbeanid (String beanid) {this.beanid = beanid;} public StringclassNam Implclassname) {this.implclassname = inmplassname;} public boolean issingl () {return issingl;} public void setsingle (boolean issingl) {this.issingl = issingl 4.Bean 的创建类 : Бианхауристиль
/** * *//package com.cxm.bean;/** * @author admin * */public class beanutil {private static xmlbeanfactory factory = new xmlbeanfactory ();/** * 获取定义好的 Bean 对象 * @param p_beanid * @return * @Throhs Exception */public Static CreateBean (строка P_Beanid) factory.createbean (p_beanid);}} /** * * * /package com.cxm.bean; импорт java.io.filenotfoundexception; импорт java.io.inputstream; импорт java.lang.reflect.constructor; импорт java.util.hashmap; com.cxm.beaninfo.beaninfo; import com.cxm.exception.nosuchbeandefinitionexception; import com.cxm.xmlutil.xmlutils;/** * @author admin * */public class xmlbeanfactory {private static String bean_xml = "/beans.xmlaTe Beandefmap = new Hashmap <String, beaninfo> (); частная статическая карта <string, object> instancemap = new Hashmap <String, object> (); static {inputstream in = xmlbeanfactory.class.getResourceasStream (bean_xml); if (in == null) (FilenotFoundException e) {e.printStackTrace ();}} stringBuffer sb = new StringBuffer (); try {xmlutils.parsexmldef (in, beandefmap, sb);} catch (Exception e) {thress new Runtimeexcept () null exection exection null) || beanid.trim () == "") {бросить новое исключение ("beanid не может быть null или ''");} beaninfo beaninfo = beandefmap.get (beanid); if (null == beaninfo) {бросить новый nosuchbeanditionexexexexcept exante; if (beaninfo.issingl ()) {exance = instancemap.get (beanid); if (null! = exaction) {return Encement;}} string inmplass = beaninfo.getimplclassname (); constructor <?> constructor = class.forname (implass.trim (). constructor.newinstance (); if (beaninfo.issingl ()) {instancemap.put (beanid, exante);} return ancess;}} 5. 测试 :
/** * *//пакет com.cxm.test;/** * @author admin * */public interface syshello {void syshello ();}/** * */package com.cxm.test;/** * @author admin * */public class syshelloimples World! ");}}/** * *//package com.cxm.test; import com.cxm.bean.beanutil;/** * @author admin * */public class test {/** * @param args * @throws */public void main (string [] args) throhs {syshello s = (Syshello) beanatil.createbean ("syshelloimpl"); s.syshello ();}}以上这篇 Java 实现 Spring 在 Xml 配置 java 类的方法就是小编分享给大家的全部内容了 , 希望能给大家一个参考 , 也希望大家多多支持武林网。 也希望大家多多支持武林网。 也希望大家多多支持武林网。