自己重写Spring_XmlBeanFactory
2007年11月25日 星期日 01:53
自己重写Spring_XmlBeanFactory
-----------------------------------------------------------------------------------------
HelloWorld.java
-----------------------------------------------------------------------------------------
public interface HelloWorld {
void setName(String name);
String say();
}
-----------------------------------------------------------------------------------------
HelloWorldImpl.java
-----------------------------------------------------------------------------------------
public class HelloWorldImpl implements HelloWorld {
private String >
public void setName(String name) {
this.name = name;
}
public String say() {
if (name == null)
return "Hello,world!";
return "Hello," + name + "!";
}
}
-----------------------------------------------------------------------------------------
SpringStartup.java
-----------------------------------------------------------------------------------------
public class SpringStartup {
/**
* @param args
*/
public static void main(String[] args) {
XmlBeanFactory factory = new XmlBeanFactory("beans.xml");
HelloWorld hello = (HelloWorld) factory.getBean("a");
System.out.println(hello.say());
}
}
-----------------------------------------------------------------------------------------
beans.xml
-----------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<beans>
<bean >
<property value="a" />
</bean>
<bean >
<property value="xiong" />
</bean>
</beans>
-----------------------------------------------------------------------------------------
XmlBeanFactory.java
-----------------------------------------------------------------------------------------
import java.lang.reflect.Method;
import java.util.Properties;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class XmlBeanFactory {
private Properties pro;
private Document document = null;
private static String XMLFILE = null;
private static String key = "key";
public XmlBeanFactory(String XMLFILE) {
this.XMLFILE = XMLFILE;
init();
}
private void init() {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(XMLFILE);
} catch (Exception ex) {
ex.printStackTrace();
}
}
// 解析XML文件
private Properties xmlParser(String nodeName, String id_key, String id_value) {
Properties pro = new Properties();
try {
NodeList list = document.getElementsByTagName(nodeName);
for (int k = 0; k < list.getLength(); k++) {
Node fatherNode = list.item(k);
NamedNodeMap attributes = fatherNode.getAttributes();
//
boolean flag = false;
for (int i = 0; i < attributes.getLength(); i++) {
Node attribute = attributes.item(i);
if (attribute.getNodeName().equals(id_key)
&& attribute.getNodeValue().equals
(id_value)) {
flag = true;
}
}
if (!flag)
continue;
//
for (int i = 0; i < attributes.getLength(); i++) {
Node attribute = attributes.item(i);
pro.setProperty(attribute.getNodeName(), attribute
.getNodeValue());
}
//
NodeList childNodes = fatherNode.getChildNodes();
for (int j = 0; j < childNodes.getLength(); j++) {
Node childNode = childNodes.item(j);
if (childNode instanceof Element) {
NamedNodeMap child_attributes = childNode
.getAttributes();
for (int i = 0; i <
child_attributes.getLength(); i++) {
Node child_attribute =
child_attributes.item(i);
pro.setProperty
(child_attribute.getNodeName(),
child_attribute.getNodeValue());
}
}
}
break;
}
return pro;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
public Object getBean(String id_value) {
String nodeName = "bean";
Properties pro = xmlParser(nodeName, "id", id_value);
return ClassLoadFactory.loadClassByConstructor(pro);
}
}
/** 类装载工厂 */
class ClassLoadFactory {
public static Object loadClassByConstructor(Properties pro) {
try {
Class cl = Class.forName(pro.getProperty("class"));
Object obj = cl.newInstance();
Class[] p = { String.class };
//
String name");
String methodName = name.substring(0, 1).toUpperCase()
+ name.substring(1, name.length());
Method method = cl.getMethod("set" + methodName, p);
Object[] value = { pro.getProperty("value") };
method.invoke(obj, value);
//
return obj;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
}
-----------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------
HelloWorld.java
-----------------------------------------------------------------------------------------
public interface HelloWorld {
void setName(String name);
String say();
}
-----------------------------------------------------------------------------------------
HelloWorldImpl.java
-----------------------------------------------------------------------------------------
public class HelloWorldImpl implements HelloWorld {
private String >
public void setName(String name) {
this.name = name;
}
public String say() {
if (name == null)
return "Hello,world!";
return "Hello," + name + "!";
}
}
-----------------------------------------------------------------------------------------
SpringStartup.java
-----------------------------------------------------------------------------------------
public class SpringStartup {
/**
* @param args
*/
public static void main(String[] args) {
XmlBeanFactory factory = new XmlBeanFactory("beans.xml");
HelloWorld hello = (HelloWorld) factory.getBean("a");
System.out.println(hello.say());
}
}
-----------------------------------------------------------------------------------------
beans.xml
-----------------------------------------------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<beans>
<bean >
<property value="a" />
</bean>
<bean >
<property value="xiong" />
</bean>
</beans>
-----------------------------------------------------------------------------------------
XmlBeanFactory.java
-----------------------------------------------------------------------------------------
import java.lang.reflect.Method;
import java.util.Properties;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class XmlBeanFactory {
private Properties pro;
private Document document = null;
private static String XMLFILE = null;
private static String key = "key";
public XmlBeanFactory(String XMLFILE) {
this.XMLFILE = XMLFILE;
init();
}
private void init() {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(XMLFILE);
} catch (Exception ex) {
ex.printStackTrace();
}
}
// 解析XML文件
private Properties xmlParser(String nodeName, String id_key, String id_value) {
Properties pro = new Properties();
try {
NodeList list = document.getElementsByTagName(nodeName);
for (int k = 0; k < list.getLength(); k++) {
Node fatherNode = list.item(k);
NamedNodeMap attributes = fatherNode.getAttributes();
//
boolean flag = false;
for (int i = 0; i < attributes.getLength(); i++) {
Node attribute = attributes.item(i);
if (attribute.getNodeName().equals(id_key)
&& attribute.getNodeValue().equals
(id_value)) {
flag = true;
}
}
if (!flag)
continue;
//
for (int i = 0; i < attributes.getLength(); i++) {
Node attribute = attributes.item(i);
pro.setProperty(attribute.getNodeName(), attribute
.getNodeValue());
}
//
NodeList childNodes = fatherNode.getChildNodes();
for (int j = 0; j < childNodes.getLength(); j++) {
Node childNode = childNodes.item(j);
if (childNode instanceof Element) {
NamedNodeMap child_attributes = childNode
.getAttributes();
for (int i = 0; i <
child_attributes.getLength(); i++) {
Node child_attribute =
child_attributes.item(i);
pro.setProperty
(child_attribute.getNodeName(),
child_attribute.getNodeValue());
}
}
}
break;
}
return pro;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
public Object getBean(String id_value) {
String nodeName = "bean";
Properties pro = xmlParser(nodeName, "id", id_value);
return ClassLoadFactory.loadClassByConstructor(pro);
}
}
/** 类装载工厂 */
class ClassLoadFactory {
public static Object loadClassByConstructor(Properties pro) {
try {
Class cl = Class.forName(pro.getProperty("class"));
Object obj = cl.newInstance();
Class[] p = { String.class };
//
String name");
String methodName = name.substring(0, 1).toUpperCase()
+ name.substring(1, name.length());
Method method = cl.getMethod("set" + methodName, p);
Object[] value = { pro.getProperty("value") };
method.invoke(obj, value);
//
return obj;
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
}
-----------------------------------------------------------------------------------------
本篇日志被作者设置为禁止发表新评论
©2008 Baidu
引文来源 自己重写Spring_XmlBeanFactory_熊熊之家