1. 创建自己的 bean 创建自己的 : beans.xml
<? xml version = "1.0"encoding = "utf-8"?> <susi-beans> <beans> <bean id = "syshelloimpl"type = "com.cxm.test.syshello"> <sc> test </desc> <app com.cxm.test.syshelloimpl </emplcass> </boan> </boon>
2. 提供解析 xml 类 : xmlutils
/** * */패키지 com.cxm.xmlutil; import java.io.inputStream; import java.util.iterator; import java.util.map; import org.jdom.attribute; import org.jdom.document; import org.jdom.element; import org.jdom.input.input.input.input.input.saxbuilder; com.cxm.beaninfo.beaninfo;/** * @author admin * */public class xmlutils {public static void parsexmldef (inputstream in, map <string, beaninfo> beandefmap, Stringbuffer sb) 예외 {saxbuilder = new saxbuilder (거짓); 거짓); reader.build (in); iterator beanit = xpath.selectnodes (doc, "/busi-beans/beans/bean"). irtherator(); element e; beaninfo beandef; while (beanit.hasnext ()) {beandef = new beaninfo (); e = (Element) beant.next (); e.getattrid = e. attrtype = e.getAttribute ( "type"); attribute singletype = e.getAttribute ( "single"); boolean issingle = true; if (null! = singletype && "1".equals (singletype.getValue ()) {issingle = false;} beandef.setsingle (issingle); beandef.setbeanid (attrid.getValue ()); beandef.settype (attrtype.getValue ()); beandef.setBeandesc (e, "desc"); beandef.setimplastname (e, e, "empt-class"); // 处理初始化参数 处理初始化参数 beandefmap.put (attrid.getValue (), beandef);}} catch (예외 e) {e.printstacktrace ();}}/** * 根据指定的 根据指定的 根据指定的 xpath 获取 * * @param p_lement * @param p_xpath * @Throws */the getrows */the getrows */the getrows */the get getrows */xpath. p_Element, String p_xpath) 예외 {string text = null; element e = (element) xpath.selectsingLenode (p_element, p_xpath); if (e! = null) {text = e.getText ();} else} report text;}}. 3. Bean io
/** * */package com.cxm.beanInfo;/** * @Author admin * */public class beanInfo {private String beanid; private String 유형; private String beandesc; public String getBeandesc () {return beandesc;} public void setbeandesc (String beandesc) {this.beandesc = beandesc;} public gettype (retype) settype (String type) {this.type = type;} private String emprclassName; public String getBeanid () {return beanid;} public void setBeanid (String Beanid) {this.beanid = beanid;} public string getImplclassName (return implcassName;} public void setimplclassname inmpclassName) {this.implclassName = inmpclassName;} public boolean issingle () {return issingle;} public void setsingle (boolean issingle) {this.issingle = issingle;} private boolean issingle = true;} 4.Bean : beanutil
/** * */package com.cxm.bean;/** * @author admin * */public class beanutil {private static xmlbeanfactory factory = new xmlbeanfactory ();/** * 获取定义好的 bean 对象 * @param p_beanid * @Throws 예외 */public strows reston (string p_beanid). 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; java.map; com.cxm.beaninfo.beaninfo; import com.cxm.exception.nosuchbeandefinitionException; import com.cxm.xmlutil.xmlutils;/** * @author admin */public class xmlbeantory {private static string bean_xml = "/beans.xml"; BeanDefMap = new Hashmap <String, BeanInfo> (); private static map <string, object> instancemap = new Hashmap <string, object> (); static {inputStream in = xmlBeanCefory.class.getResourceasStream (in = inull); (filenotfoundException e) {e.printstacktrace ();}} stringbuffer sb = new StringBuffer (); try {xmlutils.parsexmldef (in, beandefmap, sb);} catch (예외 e) {new runtimeexcection ();}} {string beanid | if (string beanid) | Beanid.trim () == "") {Throw New Exception ( "Beanid는 null 또는 '' '");} beaninfo beaninfo = beandefmap.get (beanid); if (null == BeanInfo) { "Beanid 인스턴스가 xml에서 정의되지 않음"); = instancemap.get (beanid); if (null! = instance) {return instance;}} string emprclass = beanInfo.getImplClassName (); 생성자 <?> constructor = class.forname (emplClass.trim ()). getConstructor (); 생성자 .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 syshello {@overridepublic void syshello () {upid.public systallo (). World! ");}}/** * */package com.cxm.test; import com.cxm.bean.beanutil;/** * @author admin */public class test {/** * @param args */public static void main (string [] args) 예외 {syshello s = (Syshello) Beanutil.createbean ( "Syshelloimpl"); s.syshello ();}}以上这篇 Java a 스프링 在 xml 配置 java 类的方法就是小编分享给大家的全部内容了 类的方法就是小编分享给大家的全部内容了, 希望能给大家一个参考, 也希望大家多多支持武林网。 也希望大家多多支持武林网。