首页 > 代码库 > 自己写Spring框架(1)—小文版ApplicationContext容器

自己写Spring框架(1)—小文版ApplicationContext容器

直接上源码:

beans.xml

1 <?xml version="1.0" encoding="UTF-8"?>2 <beans xmlns="http://www.springframework.org/schema/beans"3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"4        xsi:schemaLocation="http://www.springframework.org/schema/beans5            http://www.springframework.org/schema/beans/spring-beans.xsd">6     <bean id="personServiceBean" class="service.imp.PersonServiceBean"></bean>7 </beans>

PersonService.java

package service;public interface PersonService {    public void sava();}

PersonServiceBean.java

 1 package service.imp; 2  3 import service.PersonService; 4  5  6 public class PersonServiceBean implements PersonService  { 7      8     public void sava(){ 9         System.out.println("执行save方法");10     }11 }

BeanDefinition.java

 1 package junit.test; 2  3 public class BeanDefinition { 4     private String idValue;//保存id属性 5     private String classValue;//保存class属性 6     public String getIdValue() { 7         return idValue; 8     } 9     public void setIdValue(String idValue) {10         this.idValue =http://www.mamicode.com/ idValue;11     }12     public String getClassValue() {13         return classValue;14     }15     public void setClassValue(String classValue) {16         this.classValue =http://www.mamicode.com/ classValue;17     }18     public BeanDefinition(String idValue, String classValue) {19         super();20         this.idValue =http://www.mamicode.com/ idValue;21         this.classValue =http://www.mamicode.com/ classValue;22     }23     24     25 }

XiaowenApplicationContext.java

 1 package junit.test; 2  3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.List; 6 import java.util.Map; 7  8 import org.dom4j.Document; 9 import org.dom4j.Element;10 import org.dom4j.io.SAXReader;11 /***12  *任务目标:模仿Spring容器,写小文版的Spring容器 13  *步骤:    1.读取XML文件14  *        2.读取<beans> 下所有的<bean>里的id和class的属性值,将该属性保存在对象里面,然后将对象存入List里面。15  *        3.循环迭代list,读取对象里保存的id和class属性值,利用反射机制,实例class对象。将id属性与class实例对象的键值对保存在map里面。16  *        4.通过getBean(),从map里面取出对象。17  */18 public class XiaowenApplicationContext {19     private List<BeanDefinition> beanDefines = new ArrayList<BeanDefinition>();//读取id和class属性,将属性值保存在BeanDefinition对象里。20     private Map<String,Object> sigletons = new HashMap<String, Object>();//实例化bean,将bean保存在map里21     public XiaowenApplicationContext(String fileName) {22         this.readXML(fileName);23         this.instanceBeans();24     }25     /**实例化bean对象*/26     private void instanceBeans() {27         for (BeanDefinition beanDefinition :beanDefines) {28             try {29                 String idValue = http://www.mamicode.com/beanDefinition.getIdValue();//读取id属性30                 String classValue = http://www.mamicode.com/beanDefinition.getClassValue();//读取class属性31                 if(classValue!=null&&!"".equals(classValue.trim())){32                     sigletons.put(idValue, Class.forName(classValue).newInstance());33                 }34             }catch (Exception e) {35                 e.printStackTrace();36             }37         }38     }39     /**从map中获取bean对象*/40     public Object getBean(String beanName){41         return sigletons.get(beanName);42     }43     44     /**运用dom4j读取XML文件*/45     private void readXML(String fileName) {46         SAXReader reader = new SAXReader();47         try {48             Document doc= reader.read("src/"+fileName);//reader()默认从工程根路径读取,而Spring的XML默认放置在src文件夹下49             Element root = doc.getRootElement();//获取根节点50             List<Element> elements = root.elements("bean");51             for (Element element : elements) {//循环迭代elements52                 String idValue = http://www.mamicode.com/element.attributeValue("id");//获取id属性53                 String classValue = http://www.mamicode.com/element.attributeValue("class");//获取class属性54                 BeanDefinition beanDefinition = new BeanDefinition(idValue, classValue);//保存id和class属性55                 beanDefines.add(beanDefinition);56             }57         } catch (Exception e) {58             e.printStackTrace();59         }60     }61 }

XiaowenSpringTest.java

 1 package junit.test; 2  3 import org.junit.Test; 4  5 import service.PersonService; 6  7 public class XiaowenSpringTest { 8     @Test 9     public void instanceSpring(){10         XiaowenApplicationContext ac = new XiaowenApplicationContext("beans.xml");11         PersonService service = (PersonService)ac.getBean("personServiceBean");12         service.sava();13     }14 }

 

自己写Spring框架(1)—小文版ApplicationContext容器