一段配置文件:
//applicationContext.XML
Java代碼
<?XML version="1.0" encoding="UTF-8"?>
<beans>
<bean id="us" class="com.youngmaster.atm.UsRate">
<property name="rate">
<value>0.8</value>
</property>
</bean>
<bean id="js" class="com.youngmaster.atm.JSRate">
<property name="rate">
<value>60</value>
</property>
</bean>
<bean id="transfer" class="com.youngmaster.atm.TransferImp">
<property name="rate">
<ref bean="JS"></ref>
</property>
</bean>
</beans>
創建一個基礎接口:
//Rate.Java
Java代碼
package com.youngmaster.atm;
/**
* @author youngmaster
* @E-mail:youngmaster.fly@gmail.com
* @version myEclipse 6.6
* @create time:Sep 15, 2009 8:55:21 AM
*/
public interface Rate {
public double getRate();
}
實現 Rate 接口1:
//UsRate.Java
Java代碼
package com.youngmaster.atm;
/**
* @author youngmaster
* @E-mail:youngmaster.fly@gmail.com
* @version myEclipse 6.6
* @create time:Sep 15, 2009 8:56:32 AM
*/
public class UsRate implements Rate {
private double rate;
public double getRate() {
return rate;
}
public void setRate(double rate) {
this.rate = rate;
}
}
實現 Rate 接口2:
//JSRate.Java
Java代碼
package com.youngmaster.atm;
/**
* @author youngmaster
* @E-mail:youngmaster.fly@gmail.com
* @version myEclipse 6.6
* @create time:Sep 15, 2009 8:57:53 AM
*/
public class JSRate implements Rate {
private double rate;
public double getRate() {
return rate;
}
public void setRate(double rate) {
this.rate = rate;
}
}
再創建一個接口,用以獲得實際取款:
//Transfer.Java
Java代碼
package com.youngmaster.atm;
/**
* @author youngmaster
* @E-mail:youngmaster.fly@gmail.com
* @version myEclipse 6.6
* @create time:Sep 15, 2009 8:58:36 AM
*/
public interface Transfer {
public double getAcount(double amount);
}
實現其接口:
//TransferImp.Java
Java代碼
package com.youngmaster.atm;
/**
* @author youngmaster
* @E-mail:youngmaster.fly@gmail.com
* @version myEclipse 6.6
* @create time:Sep 15, 2009 8:59:17 AM
*/
public class TransferImp implements Transfer{
private Rate rate;
public double getAcount(double amount) {
return amount*rate.getRate();
}
public Rate getRate() {
return rate;
}
public void setRate(Rate rate) {
this.rate = rate;
}
}
解析 XML:
//XMLBeanFactory.Java
Java代碼
package com.youngmaster.atm;
import Java.beans.IntrospectionException;
import Java.beans.PropertyDescriptor;
import Java.io.IOException;
import Java.lang.reflect.InvocationTargetException;
import Java.lang.reflect.Method;
import Javax.XML.parsers.DocumentBuilder;
import Javax.XML.parsers.DocumentBuilderFactory;
import Javax.XML.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.XML.sax.SAXException;
/**
* @author youngmaster
* @E-mail:youngmaster.fly@gmail.com
* @version myEclipse 6.6
* @create time:Sep 15, 2009 9:16:56 AM
*/
public class XMLBeanFactory {
private String file;
public XMLBeanFactory(String file) {
this.file = file;
}
public Object getBean(String id) {
Object result = null;
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
.newInstance();
try {
DocumentBuilder documentBuilder = documentBuilderFactory
.newDocumentBuilder();
Document document = documentBuilder.parse(file);
NodeList nodeList = document.getElementsByTagName("bean");
/** 遍歷XML中的bean */
for (int i = 0; i < nodeList.getLength(); i++) {
Element beanElement = (Element) nodeList.item(i);
String beanid = beanElement.getAttribute("id");
/** 通過參數中的id獲得相應的bean */
if (id.equals(beanid)) {
/** 獲取到相應的類 */
String clazz = beanElement.getAttribute("class");
Class beanclazz = Class.forName(clazz);
result = beanclazz.newInstance();
/** 獲得bean中的屬性 */
NodeList propertyList = beanElement
.getElementsByTagName("property");
/** 遍歷屬性 */
for (int j = 0; j < propertyList.getLength(); j++) {
Element propertyElement = (Element) propertyList
.item(j);
/** 獲得屬性中的 name */
String propertyname = propertyElement
.getAttribute("name");
/** 自省--非常關鍵 */
PropertyDescriptor propertyDescriptor = new PropertyDescriptor(
propertyname, beanclazz);
NodeList valuelist = propertyElement
.getElementsByTagName("value");
/** 獲得id 為us or JS的bean */
if (valuelist != null && valuelist.getLength() > 0) {
Element valueelement = (Element) valuelist.item(0);
String nodvalue = valueelement.getFirstChild()
.getNodeValue();
Method writemethod = propertyDescriptor
.getWriteMethod();
writemethod.invoke(result, Double
.parseDouble(nodvalue));
}
else {
/** 獲得id 為transfer的bean */
NodeList reflist = propertyElement
.getElementsByTagName("ref");
Element refelement = (Element) reflist.item(0);
String beanid1 = refelement.getAttribute("bean");
/** 遞歸調用 */
Object tempobject = this.getBean(beanid1);
Method method1 = propertyDescriptor
.getWriteMethod();
method1.invoke(result, tempobject);
}
}
}
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IntrospectionException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return result;
}
}
測試類:
//AtmTest.Java
Java代碼
package com.youngmaster.atm;
/**
* @author youngmaster
* @E-mail:youngmaster.fly@gmail.com
* @version myEclipse 6.6
* @create time:Sep 15, 2009 9:01:45 AM
*/
public class AtmTest {
/**
* @param args
*/
public static void main(String[] args) {
XmlBeanFactory beanfactory = new XMLBeanFactory(
"applicationContext.XML");
Transfer transfer = (Transfer) beanfactory.getBean("transfer");
System.out.println("========================\n"+transfer.getAcount(100));
}
}
很淺顯易懂的一段代碼,當然,如果用spring實現是極其容易的;
但對此代碼的深入理解可以為學習spring的IoC起到拋磚引玉之效。