结合工厂方法,xml读取,以及JAVA反射机制写了一个spring的模拟.
各位大虾还有各位神虾,帮忙看看指出不足之处.
个人感觉对一些概念的理解还有点偏差…..望指正…
public class BeanFactoryImpl1 implements BeanFactoryInterface {
@Override
public void print() {
System.out.println("i am the BeanFactoryImpl 1 .");
}
}
package singlefactory;
public interface BeanFactoryInterface {
void print();
}
package singlefactory;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.jdom2.Document;
import org.jdom2.JDOMException;
public class CreatBeanFactory implements CreateBeanFactoryInterface {
private static CreatBeanFactory beanFactory;
private HashMap<String, Object> hashMap = null; // 取得反射后的类引用及getBean时的名称(id)
/**
*工厂类
*/
public static CreatBeanFactory getBeanFactory() {
if (null == beanFactory) {
beanFactory = new CreatBeanFactory();
}
return beanFactory;
}
//读取xml文件
public CreatBeanFactory build(String xml) throws ClassNotFoundException,
InstantiationException, IllegalAccessException, JDOMException,
IOException {
Document doc = null;
doc = (Document) new JDom().readXML(new BeanFactoryTest().getClass(),
xml);
hashMap = getReflect(JDom.getIdAndClassName(doc));
return beanFactory;
}
//取得xml文件对应的类引用和其id
private HashMap<String, Object> getReflect(HashMap<String, String> map)
throws ClassNotFoundException, InstantiationException,
IllegalAccessException {
HashMap<String, Object> reflect = new HashMap<String, Object>();
Set<Map.Entry<String, String>> set = map.entrySet();
Iterator<?> it = set.iterator();
while (it.hasNext()) {
Map.Entry<String, String> entry = (Entry<String, String>) it.next();
String id = entry.getKey();
Object obj = getClassByReflect(entry.getValue());
reflect.put(id, obj);
}
return reflect;
}
private Object getClassByReflect(String className)
throws ClassNotFoundException, InstantiationException,
IllegalAccessException {
Class<?> cls = null;
cls = Class.forName(className);
return cls.newInstance();
}
//getBean方法
public Object getBean(String id) {
return hashMap.get(id);
}
}
package singlefactory;
public interface CreateBeanFactoryInterface {
Object getBean(String id);
}
package singlefactory;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
public class JDom {
public Document readXML(Class<?> classs, String xmlName)
throws JDOMException, IOException {
SAXBuilder sb = new SAXBuilder();
Document doc = sb.build(classs.getClassLoader().getResourceAsStream(
xmlName)); // 构造文档对象
return doc;
}
public static HashMap<String, String> getIdAndClassName(Document doc) {
HashMap<String, String> map = new HashMap<String, String>();
Element root = doc.getRootElement();
// 取名字为bean的所有元素
List<Element> list = root.getChildren();
for (int i = 0; i < list.size(); i++) {
Element element = list.get(i);
String id = element.getAttributeValue("id");
String className = element.getAttributeValue("class");
map.put(id, className);
}
return map;
}
}
// xml文件
<?xml version="1.0" encoding="UTF-8"?>
<factory>
<bean id="BeanFactoryImpl1" class="singlefactory.BeanFactoryImpl1"></bean>
<bean id="BeanFactoryImpl2" class="singlefactory.BeanFactoryImpl2"></bean>
<bean id="BeanFactoryImpl3" class="singlefactory.BeanFactoryImpl3"></bean>
</factory>
package singlefactory;
import java.io.IOException;
import org.jdom2.JDOMException;
import org.junit.Test;
//JUnit Test
public class BeanFactoryTest {
@Test
public void test() throws JDOMException, IOException,
ClassNotFoundException, InstantiationException,
IllegalAccessException {
CreateBeanFactoryInterface in = CreatBeanFactory.getBeanFactory()
.build("factory.xml");
BeanFactoryInterface a = (BeanFactoryInterface) in
.getBean("BeanFactoryImpl2");
a.print();
}
}