1. 创建自己的 Bean 文件 : Beans.xml
<? Xml Version = "1.0" encoding = "UTF-8"?> <Busi-beans> <Bacs> <bean id = "SyshelloImpl" type = "com.cxm.test.syshello"> <desc> </desc> <emplass> com.cxm.test.
2. 提供解析 xml 类 : xmlutils
/** * */paket com.cxm.xmlutil; import java.io.inputstream; import java.util.iterator; import java.util.map; impor org.jdom.attribute; impor org.jdom.document; impor org.jdom.element; impor org.jdom.inpom.input.input.document; com.cxm.beaninfo.beaninfo;/** * @author admin * */kelas publik xmlutils {public static void parsexmldef (inputStream in, peta <string, beaninfo> beandefmap, stringbuffer sb) lempar pengecuali reader.build (dalam); iterator beanit = xpath.selectnodes (doc, "/busi-beans/beans/bean") e.getAttribute ("id"); attribute attrtype = e.getAttribute ("type"); atribut singletype = e.getAttribute ("single"); boolean issingle = true; if (null! = singletype && "1. false;} beandef.setsingle (issingle); beandef.setBeanId (attrid.getValue ()); beandef.settype (attrtype.getValue ()); beandef.setbeandesc (getText (e, "desc")); beanDef.setimclapsn (getText ("desc")); beanDef.setimplexn (getText ("desc")); beanDef.setimply "ImP-Class"); // 处理初始化参数 beandefmap.put (attrid.getValue (), beandef);}} catch (pengecualian e) {e.printstacktrace ();}}/** * 根据指定的 elemen, xpath @xml 文档内容 * * @param p_element * @param * @prowse @trows @trows * @prows * getText (elemen p_element, string p_xpath) melempar pengecualian {string text = null; elemen e = (elemen) xpath.selectsinglenode (p_element, p_xpath); if (e! = null) {text = e.getText ();} else {} return teks;}} 3. 定义 Bean 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 setBeanDesc(String beanDesc){this.beanDesc = beanDesc;}public String getType(){return 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 getImplAslassName () {this.beanid = beanid;} public string getImplAsPLASSNAME () {this.beanid = beanid; ImprlassName) {this.implClassName = ImprlassName;} public boolean issingle () {return issingle;} public void setsingle (boolean issingle) {this.issingle = issingle;} private boolean issingle = true;} 4.Bean : : Beanutil
/** * */paket com.cxm.bean;/** * @author admin * */kelas publik beanutil {private static xmlbeanfactory factory = new xmlbeanfactory ();/** * 获取定义好的 bean 对象 * @param p_beanid * @return * @throws Exception {Public Static ObybeInd) @Thows Exception {Public Exception {Public ObybeanD) @Thows Exception * factory.createBean (p_beanid);}} /** * * /package com.cxm.bean; import java.io.filenotfoundException; import java.io.inputStream; import java.lang.reflect.constructor; impor java.util.hashmaap; com.cxm.beaninfo.beaninfo; import com.cxm.exception.nosuchbeandefinitionException; import com.cxm.xmlutil.xmlutils;/** * @Author admin */kelas publik. beandefmap = hashmap baru <string, beanInfo> (); peta statis privat <string, objek> instanceMap = hashmap baru <string, objek> (); static {inputStream in = xmlbeanfactory.class.getResourCeasStream (bean_xml); iflbean. FileNotFoundException();}catch (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) melempar pengecualian {if (beanid == null || beanid.trim () == "") {lempar pengecualian baru ("beanid tidak bisa null atau ''");} beaninfo beaninfo = beandefmap.get (beanid); if (null == beaninfo) {lempar nosuchbeDefinitionException ("beanid tidak) {lempar nosuchbeDefinitionException (" beanid tidak "nosuchbeFion (" beanid tidak "nosuchbeDefinitionException (" beanid tidak "nosuchbeDeedefinition (" beanid noSuchbePine ("beanid noSuchbePine (" beanid tidak "nosuchbeDe) instance; if (beanInfo.issingle ()) {instance = instanceMap.get (beanid); if (null! = instance) {return instance;}} string implClass = beaninfo.getImplClassName (); konstruktor <?> constructor = class.forname (impllass.trim ()); konstruktor <?> classor (class.forname (impllass.trim ()); constructor.newinstance (); if (beaninfo.issingle ()) {instanceMap.put (beanid, instance);} return instance;}} 5. : :
/** * */paket com.cxm.test;/** * @author admin * */antarmuka publik Syshello {void syshello ();}/** */package com.cxm.test;/** * @Author */class public SyshelloImpl mengimplementasikan syshello {@overridepublo (eoverridepublo {@Overridepublo {@OverRidepublo {@Overridepublo {@Overridepublo {@Overridepublo. world!");}} /** * */package com.cxm.test;import com.cxm.bean.BeanUtil;/** * @author admin * */public class Test{/** * @param args * @throws Exception */public static void main(String[] args) throws Exception{SysHello s = (Syshello) beanutil.createBean ("SyshelloImpl"); s.shello ();}}以上这篇 Java 实现 Spring 在 XML 配置 Java 类的方法就是小编分享给大家的全部内容了 , 希望能给大家一个参考 , ,