国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

java實(shí)現(xiàn)Spring在XML配置java類的方法

2019-11-26 13:30:51
字體:
供稿:網(wǎng)友

1. 創(chuàng)建自己的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

/** *  */package 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.SAXBuilder;import org.jdom.xpath.XPath;import com.cxm.beaninfo.BeanInfo;/** * @author admin *  */public class XmlUtils{		public static void parseXmlDef(InputStream in, Map<String,BeanInfo> beanDefMap,			StringBuffer sb) throws Exception	{		SAXBuilder reader = new SAXBuilder(false);		Document doc = null;		try		{			doc = reader.build(in);			Iterator beanIt = XPath.selectNodes(doc, "/busi-beans/beans/bean")					.iterator();			Element e;			BeanInfo beanDef;			while (beanIt.hasNext())			{				beanDef = new BeanInfo();				e = (Element) beanIt.next();				Attribute attrId = e.getAttribute("id");				Attribute 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(getText(e, "desc"));				beanDef.setImplClassName(getText(e, "impl-class"));				//處理初始化參數(shù)				beanDefMap.put(attrId.getValue(), beanDef);			}		}		catch (Exception e)		{			e.printStackTrace();		}	}		/**	 * 根據(jù)指定的element, xPath獲取XML文檔內(nèi)容	 * 	 * @param p_element	 * @param p_xPath	 * @return	 * @throws Exception	 */	public static String getText(Element p_element, String p_xPath)			throws Exception {		String text = null;		Element e=(Element)XPath.selectSingleNode(p_element,p_xPath);		if (e != null) {			text = e.getText();		} else {		}		return text;	}}

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 getImplClassName()	{		return implClassName;	}	public void setImplClassName(String implClassName)	{		this.implClassName = implClassName;	}	public boolean isSingle()	{		return isSingle;	}	public void setSingle(boolean isSingle)	{		this.isSingle = isSingle;	}	private boolean isSingle = true;	}

4.bean的創(chuàng)建類:BeanUtil

/** *  */package com.cxm.bean;/** * @author admin * */public class BeanUtil{	private static XmlBeanFactory factory = new XmlBeanFactory();	/**	 * 獲取定義好的Bean對象	 * @param p_beanId	 * @return	 * @throws Exception 	 */	public static Object createBean(String p_beanId)			throws Exception {		return 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;import java.util.Map;import 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.xml";		private static Map<String,BeanInfo> beanDefMap = new HashMap<String,BeanInfo>();		private static Map<String,Object> instanceMap = new HashMap<String,Object>();		static {		InputStream in = XmlBeanFactory.class.getResourceAsStream(BEAN_XML);		if(in ==null){			try{				throw new 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) throws Exception{		if(beanId==null || beanId.trim()==""){			throw new Exception("BeanId can not NULL or '' ");		}		BeanInfo beanInfo = beanDefMap.get(beanId);		if(null ==beanInfo ){			throw new NoSuchBeanDefinitionException(" beanid is not define in xml");		}		Object instance;		if(beanInfo.isSingle()){			instance =instanceMap.get(beanId);			if(null != instance){				return instance;			}		}		String implClass = beanInfo.getImplClassName();		Constructor<?> constructor = Class.forName(implClass.trim()).getConstructor();		instance = constructor.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 SysHelloImpl implements SysHello{	@Override	public void sysHello()	{		System.out.println("hello 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.sysHello();	}	}

以上這篇java實(shí)現(xiàn)Spring在XML配置java類的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 浏阳市| 镇巴县| 宣威市| 宿州市| 日照市| 青浦区| 寿阳县| 罗江县| 泰顺县| 台南县| 满洲里市| 福州市| 宝鸡市| 华宁县| 福鼎市| 甘谷县| 嘉义市| 安龙县| 外汇| 伊宁县| 道孚县| 宁化县| 丹巴县| 普洱| 长子县| 海口市| 桑日县| 威宁| 清苑县| 顺平县| 丘北县| 马尔康县| 个旧市| 仪征市| 侯马市| 弥渡县| 达孜县| 儋州市| 大冶市| 冷水江市| 遵义市|