spring的BeanFactory原理模擬

結合工廠方法,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();
 }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章