1.xml文件
<?xml version="1.0" encoding="UTF-8"?> <beans> <bean id="person" class="com.jd.xq.spring.bean.Person"/> </beans>
2.XqClassPathXmlApplicationContext
(1)读取xml文件
(2)实例化bean—用反射的方式
package com.jd.xq.spring.context; import com.jd.xq.spring.bean.BeanDefinition; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.SAXReader; import java.net.URL; import java.util.Iterator; import java.util.*; /** * @author * @Date 2019-12-27 11:38 **/ public class XqClassPathXmlApplicationContext { private List<BeanDefinition> beanDefines = new ArrayList<BeanDefinition>();//用来存储所有的beans private Map<String, Object> sigletons = new HashMap<String, Object>();//用来存储实例化后的bean /** * 构造方法,用来模拟spring的行为 * * @param fileName */ public XqClassPathXmlApplicationContext(String fileName) { //1.read xml this.readXml(fileName); //2.实例化bean this.instanceBeans(); } /** * 根据文件名读取xml的配置文件 * * @param fileName * */ private void readXml(String fileName) { // TODO Auto-generated method stub //创建一个读取器 SAXReader saxReader = new SAXReader(); Document document = null; try { //获取要读取的配置文件的路径 URL xmlPath = this.getClass().getClassLoader().getResource(fileName); //读取文件内容 document = saxReader.read(xmlPath); //获取xml中的根元素 Element rootElement = document.getRootElement(); for (Iterator iterator = rootElement.elementIterator(); iterator.hasNext(); ) { Element element = (Element) iterator.next(); String id = element.attributeValue("id");//获取bean的id属性值 String clazz = element.attributeValue("class");//获取bean的class属性值 BeanDefinition beanDefinition = new BeanDefinition(id, clazz); beanDefines.add(beanDefinition); } } catch (Exception e) { e.printStackTrace(); } } /** * 实例化bean */ private void instanceBeans() { if (beanDefines != null && beanDefines.size() > 0) { //对每个bean进行实例化 for (BeanDefinition beanDefinition : beanDefines) { try { //bean的class属性存在的时候才进行实例化,否则不进行实例化 if (beanDefinition.getClassName() != null && !beanDefinition.getClassName().equals("")) { //实例化的关键操作 sigletons.put(beanDefinition.getId(), Class.forName(beanDefinition.getClassName()).newInstance()); System.out.println("id为:" + beanDefinition.getId() + "的bean实例化成功"); } } catch (Exception e) { System.out.println("bean实例化失败"); e.printStackTrace(); } } } } public Object getBean(String beanName) { return sigletons.get(beanName); } }
3.解析xml工具类
package com.jd.xq.spring.utils; import java.net.URL; import java.util.Iterator; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.SAXReader; /** * @author * @Date 2019-12-27 11:40 **/ public class XmlParseUtil { @SuppressWarnings("rawtypes") public static void parseXml(String xmlPath) { SAXReader saxReader = new SAXReader(); Document document = null; try { document = saxReader.read(xmlPath); //获取根节点 Element elementRoot = document.getRootElement(); System.out.println("根节点名称:" + elementRoot.getName()); //遍历根节点下面的节点 for (Iterator elementIterator = elementRoot.elementIterator(); elementIterator.hasNext(); ) { Element element = (Element) elementIterator.next(); //获取节点的信息,,包括节点的名称以及节点的属性信息 System.out.println("节点:" + element.getName()); for (Iterator attrIterator = element.attributeIterator(); attrIterator.hasNext(); ) { Attribute attr = (Attribute) attrIterator.next(); //节点的属性信息 System.out.println("节点的属性名称为:" + attr.getName() + ",值为:" + attr.getValue()); } //获取节点下的子节点 for (Iterator subElementiterator = element.elementIterator(); subElementiterator.hasNext(); ) { Element subElement = (Element) subElementiterator.next(); //获取子节点的信息,包括子节点的名称以及子节点的属性信息 System.out.println("子节点:" + subElement.getName()); for (Iterator subElementAttrIterator = subElement.attributeIterator(); subElementAttrIterator.hasNext(); ) { Attribute subElementAttr = (Attribute) subElementAttrIterator.next(); //子节点的属性信息 System.out.println("子节点的属性名称为:" + subElementAttr.getName() + ",值为:" + subElementAttr.getValue()); } } } } catch (Exception e) { } } }
4.应用
package com.jd.xq.spring; import com.jd.xq.spring.context.XqClassPathXmlApplicationContext; /** * @author * @Date 2019-12-27 11:52 **/ public class SpringMain { public static void main(String[] args) { XqClassPathXmlApplicationContext xqClassPathXmlApplicationContext=new XqClassPathXmlApplicationContext("spring.xml"); xqClassPathXmlApplicationContext.getBean("person"); } }
思路总结:
1.读取、解析xml文件
2.实例化bean(反射)
3.测试
源码地址:https://github.com/zhugezifang/my-spring-framework