欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java实现Spring在XML配置java类的方法

程序员文章站 2024-03-11 22:24:49
1. 创建自己的bean文件:beans.xml ...

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

/**
 * 
 */
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"));
				//处理初始化参数
				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 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的创建类: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实现spring在xml配置java类的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。