定义一个容器,使用ConcurrentHashMap
做为单例对象的容器
- 先解析beans.xml
- 得到第一个bean对象的信息,id,class,属性和属性值
- 使用反射生成对象,并赋值
- 将创建好的bean对象放入到singletonObjects集合中
- 提供getBean(id)方法可以返回对应的bean对象
monster bean
public class Monster {
private Integer id;
private String name;
private String skill;
public Monster() {
}
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 getSkill() {
return skill;
}
public void setSkill(String skill) {
this.skill = skill;
}
@Override
public String toString() {
return "Monster{" +
"id=" + id +
", name='" + name + '\'' +
", skill='" + skill + '\'' +
'}';
}
}
package com.sparrow.spring.application;
import com.sparrow.spring.bean.Monster;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.File;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
/**
* @Author: 诉衷情の麻雀
* @Description: TODO
* @DateTime: 2023/7/24 8:09
**/
public class SpaApplicationContext {
private ConcurrentHashMap<String, Object> singletonObjects = new ConcurrentHashMap<>();
//构造器 接收一个容器的配置文件
public SpaApplicationContext(String iocBeanXmlFile) throws Exception {
//1. 得到类加载路径
String path = this.getClass().getResource("/").getPath();
//2.创建SaxReader
SAXReader saxReader = new SAXReader();
//3.得到Document对象
Document document = saxReader.read(new File(path + iocBeanXmlFile));
//4.得到rootElement
Element rootElement = document.getRootElement();
List<Element> elements = rootElement.elements("bean");
for (Element bean : elements) {
String id = bean.attributeValue("id");
String classFullPath = bean.attributeValue("class");
Integer monsterId = null;
String name = "";
String skill = "";
//遍历bean下面的property属性
List<Element> property = bean.elements("property");
for (Element elementProperty : property) {
//如果是id把值存起来
if ("id".equalsIgnoreCase(elementProperty.attributeValue("name"))) {
monsterId = Integer.valueOf(elementProperty.attributeValue("value"));
} else if ("name".equalsIgnoreCase(elementProperty.attributeValue("name"))) {
name = elementProperty.attributeValue("value");
} else if ("skill".equalsIgnoreCase(elementProperty.attributeValue("name"))) {
skill = elementProperty.attributeValue("value");
}
//利用反射 根据类的全路径 进行实例化
Class<?> aClass = Class.forName(classFullPath);
Monster o = (Monster) aClass.newInstance();
o.setId(monsterId);
o.setName(name);
o.setSkill(skill);
//放入容器中
singletonObjects.put(id, o);
}
}
}
public Object getBean(String id) {
return singletonObjects.get(id);
}
}
beans.xml
<?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.xsd">
<bean class="com.sparrow.spring.bean.Monster" id="monster01">
<property name="id" value="1"/>
<property name="name" value="白骨精"/>
<property name="skill" value="吃唐僧"/>
</bean>
<bean class="com.sparrow.spring.bean.Monster" id="monster02">
<property name="id" value="2"/>
<property name="name" value="老鼠精"/>
<property name="skill" value="吸血"/>
</bean>
</beans>
public class Test {
public static void main(String[] args) throws Exception {
SpaApplicationContext spaApplicationContext = new SpaApplicationContext("beans.xml");
Monster monster02 = (Monster) spaApplicationContext.getBean("monster02");
Monster monster01 = (Monster) spaApplicationContext.getBean("monster01");
System.out.println(monster02);
System.out.println(monster01);
}
}