手写SpringIOC的XML实现方式

1.XML文件(Spring.xml)



	

2.需要注入的实体Bean类

package com.weichai.entity;

import java.io.Serializable;

/**
 * User实体诶
 * @author linhaiy
 * @date 2019.01.24
 */
public class User implements Serializable {

	private Integer id;
	private String name;
	private String password;
	private Integer age;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", password=" + password + ", age=" + age + "]";
	}
	
}

3.Bean注入的实现原理

package com.weichai.spring;

import java.io.File;
import java.io.InputStream;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
    *     手写SpringIOC注入Bean的XML实现方式
 * @author linhaiy
 * @date 2019.01.24
 */
public class ClassPathXmlApplicationContext {

	private String xmlPath;           //要读取的xml路径地址

	public ClassPathXmlApplicationContext(String xmlPath) {
		super();
		this.xmlPath = xmlPath;
	}
	
	/**
	   * 依赖注入获取Bean的主体类
	 * @param beanId
	 * @return
	 * @throws Exception
	 */
	public Object getBean(String beanId) throws Exception {
		//1.读取配置文件
		List elements = readerXml();
		if(elements == null) {
			throw new Exception("该配置文件没有子元素");
		}
		//2.使用beanId查找对应的class地址
		String beanClass = findXmlByIDClass(elements, beanId);
		if(StringUtils.isEmpty(beanClass)) {
			throw new Exception("未找到对应的class地址");
		}
		//3.使用反射机制初始化,对象
		Class forName = Class.forName(beanClass);
		return forName.newInstance();
	}

	/**
	    *   使用beanid查找该Class地址
	 * @param elements
	 * @param beanId
	 * @return
	 */
	private String findXmlByIDClass(List elements, String beanId) throws Exception {
		// TODO Auto-generated method stub
		for(Element element : elements) {
			// 读取节点上是否有value
			String beanIdValue = element.attributeValue("id");
			if (beanIdValue == null) {
				throw new Exception("使用该beanId未查找到元素");
			}
			if (!beanIdValue.equals(beanId)) {
				continue;
			}
			// 获取Class地址属性
			String classPath = element.attributeValue("class");
			if(!StringUtils.isEmpty(classPath)) {
				return classPath;
			}
		}
		return null;
	}

	/**
	   * 读取配置文件信息
	 * @return
	 * @throws DocumentException 
	 */
	private List readerXml() throws DocumentException {
		// TODO Auto-generated method stub
		SAXReader saxReader = new SAXReader();
		if(StringUtils.isEmpty(xmlPath)) {
			new Exception("xml路径为空...");
		}
		Document read = saxReader.read(getClassXmlInputStream(xmlPath));
		//获取根节点信息
		Element rootElement = read.getRootElement();
		//获取子节点
		List elements = rootElement.elements();
		if(elements == null || elements.isEmpty()) {
			return null;
		}
		return elements;
	}

	/**
	 * 读取xml配置文件
	 * @param xmlPath
	 * @return
	 */
	public InputStream getClassXmlInputStream(String xmlPath) {
		// TODO Auto-generated method stub
		InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream(xmlPath);
		return resourceAsStream;
	}
}

4.测试类

package com.weichai.SpringIocXml;

import com.weichai.entity.User;
import com.weichai.spring.ClassPathXmlApplicationContext;

/**
 * SpringIOC的XML实现方式测试
 * @author linhaiy
 * @date 2019.01.24
 */
public class SpringIocTest {

	public static void main(String[] args) throws Exception {
		// TODO Auto-generated method stub
		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
		User user = (User) applicationContext.getBean("user");
		System.out.println("Spring获取Bean实体的内容: " + user);
	}

}

 

你可能感兴趣的:(源码学习分析)