1.手写Spring IOC XML版本
spring xml配置
代码如下:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="user" class="com.itmayiedu.spring.entity.User"></bean>
</beans>
XML工具类
package com.itmayiedu.spring;
import java.io.InputStream;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
/**
* 手写Spring专题 XML方式注入bean
*
* @author 作者:余胜军
*
*/
public class ClassPathXmlApplicationContext {
// xml路径地址
private String xmlPath;
public ClassPathXmlApplicationContext(String xmlPath) {
this.xmlPath = xmlPath;
}
public Object getBean(String beanId) throws Exception {
// 1. 读取配置文件
List<Element> 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();
}
// 读取配置文件信息
public List<Element> readerXml() throws DocumentException {
SAXReader saxReader = new SAXReader();
if (StringUtils.isEmpty(xmlPath)) {
new Exception("xml路径为空...");
}
Document read = saxReader.read(getClassXmlInputStream(xmlPath));
// 获取根节点信息
Element rootElement = read.getRootElement();
// 获取子节点
List<Element> elements = rootElement.elements();
if (elements == null || elements.isEmpty()) {
return null;
}
return elements;
}
// 使用beanid查找该Class地址
public String findXmlByIDClass(List<Element> elements, String beanId) throws Exception {
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;
}
// 读取xml配置文件
public InputStream getClassXmlInputStream(String xmlPath) {
InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream(xmlPath);
return resourceAsStream;
}
}
spring IOC 核心类
package com.itmayiedu.spring;
import java.io.InputStream;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
/**
* 手写Spring专题 XML方式注入bean
*
*/
public class ClassPathXmlApplicationContext {
// xml路径地址
private String xmlPath;
public ClassPathXmlApplicationContext(String xmlPath) {
this.xmlPath = xmlPath;
}
public Object getBean(String beanId) throws Exception {
// 1. 读取配置文件
List<Element> 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();
}
// 读取配置文件信息
public List<Element> readerXml() throws DocumentException {
SAXReader saxReader = new SAXReader();
if (StringUtils.isEmpty(xmlPath)) {
new Exception("xml路径为空...");
}
Document read = saxReader.read(getClassXmlInputStream(xmlPath));
// 获取根节点信息
Element rootElement = read.getRootElement();
// 获取子节点
List<Element> elements = rootElement.elements();
if (elements == null || elements.isEmpty()) {
return null;
}
return elements;
}
// 使用beanid查找该Class地址
public String findXmlByIDClass(List<Element> elements, String beanId) throws Exception {
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;
}
// 读取xml配置文件
public InputStream getClassXmlInputStream(String xmlPath) {
InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream(xmlPath);
return resourceAsStream;
}
}
测试类
package com.itmayiedu.spring;
import org.dom4j.DocumentException;
import com.itmayiedu.spring.entity.User;
public class Test002 {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
User user = (User) applicationContext.getBean("user");
System.out.println(user);
}
}