首先我们要了解这个dom4j解析,dom4j是java XML的api,是一个非常优秀的xml解析API,它集成了dom,sax,jdom的优点,解析速度快,内存占用小。
我们有dom4j需要用到dom4j.jar的jar包,我这里是简单的模拟spring的IOC,是来模拟spring的beanFactory的,所以我还需要spring3.6的所有jar包,还有一个common.logging.jar包。
jar包导入完毕,创建一个标准的applicationContext.xml,去官网下一个或者copy下来,我的是这样的
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="v" class="com.test.testSpringBean.car">
</bean>
<!-- //v=com.bjsxt.spring.factory.Car -->
</beans>
创建一个测试bean对象car;
public class car {
public void run(){
System.out.println("汽车开动中。。。。。。。。");
}
}
beanFactory的接口用来管理各种bean,里面定义简单的getBean();方法
package com.test.testSpringBean;
public interface BeanFactory {
Object getBean(String id);
}
接着就是我们要解析这个applicationContext.xml文件了。
定义一个ClassPathXmlApplicationContext的java文件并且实现BeanFactory接口来模拟spring的获取这个applicationContext.xml;然后创建一个参数为name的构造方法,在构造方法里来解析xml文件;
package com.test.testSpringBean;
import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class ClassPathXmlApplicationContext implements BeanFactory{
Map<String, Object>beanMap=new HashMap<String, Object>();
public ClassPathXmlApplicationContext(String name)throws Exception{
SAXReader saxReader = new SAXReader(); //创建一个saxReader
Document doc = saxReader.read(this.getClass().getClassLoader().getResourceAsStream(name)); //读取xml文件
Element root = doc.getRootElement(); //获取根结点,我这里是beans
System.out.println("root element: " + root.getName());
List childList = root.elements();
System.out.println(childList.size());
Element beanList=root.element("bean");//获取bean结点
Object clazz=Class.forName(beanList.attributeValue("class")).newInstance();//获取bean结点的class里面的内容并且实例这个路径下的bean
beanMap.put(beanList.attributeValue("id"), clazz);//将bean对象放进map中
System.out.println(beanList);
}
public Object getBean(String id) {
// TODO Auto-generated method stub
return beanMap;
}
}
最后在main方法里面new一个BeanFactory;package com.test.testSpringBean;
BeanFactory f= new ClassPathXmlApplicationContext("applicationContext.xml");
package com.test.testSpringBean;
package com.test.testSpringBean;
import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class ClassPathXmlApplicationContext implements BeanFactory{
Map<String, Object>beanMap=new HashMap<String, Object>();
public ClassPathXmlApplicationContext(String name)throws Exception{
SAXReader saxReader = new SAXReader();
Document doc = saxReader.read(this.getClass().getClassLoader().getResourceAsStream(name));
Element root = doc.getRootElement();
System.out.println("root element: " + root.getName());
List childList = root.elements();
System.out.println(childList.size());
Element beanList=root.element("bean");
Object clazz=Class.forName(beanList.attributeValue("class")).newInstance();
beanMap.put(beanList.attributeValue("id"), clazz);
System.out.println(beanList);
}
public Object getBean(String id) {
// TODO Auto-generated method stub
return beanMap;
}
}