Java implementation Spring's method of configuring Java classes in XML _java

Source: Internet
Author: User
Tags constructor gettext xpath stringbuffer

1. Create your own bean file: 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. Provide analytic XML class: 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,beani
		Nfo> 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"));
			Process initialization Parameters Beandefmap.put (Attrid.getvalue (), beandef);
		} catch (Exception e) {e.printstacktrace (); }/** * Based on the specified element, XPath gets the contents of the XML document * * @param p_element * @param p_xpath * @return * @throws exceptio
		n */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. Define 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 creation class: Beanutil

/** * * * * * * Package com.cxm.bean;
	/** * @author Admin * */public class Beanutil {private static xmlbeanfactory factory = new Xmlbeanfactory (); /** * Get the defined Bean Object * @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 Ex
		Ception ("Beanid can not NULL or" ");
		} BeanInfo BeanInfo = Beandefmap.get (Beanid);
		if (null ==beaninfo) {throw new Nosuchbeandefinitionexception ("Beanid is isn't 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. Test:

/**
 * * * * * *
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 ();
	}
	

The above Java implementation Spring in the XML configuration Java class method is small set to share all the content, hope to give you a reference, but also hope that we support cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.