14 模拟springioc
14.1 创建java项目
14.2 创建实体类
public class Student implements Serializable{
{
System.out.println("构造代码块");
}
private Integer sid;
private String sname;
private Integer sage;
private Boolean sdy;
private Float score;
private Teacher teacher;
...
}
public class Teacher implements Serializable{
private Integer tid;
private String tname;
private String tsex;
...
}
14.3 创建核心配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans>
<bean id="stu1" class="com.zhiyou100.test01.Student">
<property name="sid" value="1001"/>
<property name="sname" value="韩梅梅"/>
<property name="sage" value="19"/>
<property name="sdy" value="true"/>
<property name="score" value="11.6"/>
</bean>
<bean id="stu2" class="com.zhiyou100.test01.Student">
<constructor-arg name="sid" value="1002"/>
<constructor-arg name="sname" value="韩寒"/>
<constructor-arg name="sage" value="19"/>
<constructor-arg name="sdy" value="false"/>
<constructor-arg name="score" value="33.5"/>
</bean>
<bean id="stu3" class="com.zhiyou100.test01.Student">
<property name="sid" value="1001"/>
<property name="sname" value="韩梅梅"/>
<property name="sage" value="19"/>
<property name="sdy" value="true"/>
<property name="score" value="11.6"/>
<property name="teacher" ref="tea1"/>
</bean>
<bean id="tea1" class="com.zhiyou100.test01.Teacher" >
<property name="tid" value="1002"/>
<property name="tname" value="高老师"/>
<property name="tsex" value="女"/>
</bean>
</beans>
14.4 创建类模拟ClassPathXmlApplicationContext
方法:1 构造方法::加载核心配置文件
方法:2 getBean方法:根据bean的id获取对象
方法:3 close方法:释放资源销毁对象
package com.zhiyou100.test01;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
public class MyClassPathXmlApplicationContext {
private Map<String, Object> mapBeans=new HashMap<String, Object>();
private InputStream in;
private Document doc;
public MyClassPathXmlApplicationContext(String fileName){
try {
in=new FileInputStream(new File("src/"+fileName));
doc=Utils.xml2Doc(in);
init();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private void init()throws Exception{
NodeList beans=doc.getElementsByTagName("bean");
for (int i = 0; i < beans.getLength(); i++) {
Element beanEle=(Element)beans.item(i);
String id=beanEle.getAttribute("id");
String className=beanEle.getAttribute("class");
Class cla=Class.forName(className);
Object obj=null;
NodeList conZiList=beanEle.getElementsByTagName("constructor-arg");
if(conZiList==null||conZiList.getLength()==0){
obj=cla.newInstance();
NodeList proZiList=beanEle.getElementsByTagName("property");
for (int j = 0; j < proZiList.getLength(); j++) {
Element proEle=(Element)proZiList.item(j);
String name=proEle.getAttribute("name");
String value=proEle.getAttribute("value");
Field field=cla.getDeclaredField(name);
field.setAccessible(true);
if(value!=null&&!value.isEmpty()){
field.set(obj, Utils.changeType(value, field.getType()));
}else{
String ref=proEle.getAttribute("ref");
field.set(obj, mapBeans.get(ref));
}
}
}else{
Class[] argsTypes=new Class[conZiList.getLength()];
Field[] fieldArr=new Field[conZiList.getLength()];
String[] valueStrArr=new String[conZiList.getLength()];
Object[] valueObjArr=new Object[conZiList.getLength()];
for (int j = 0; j <conZiList.getLength(); j++) {
Element conZi=(Element)conZiList.item(j);
fieldArr[j]=cla.getDeclaredField(conZi.getAttribute("name"));
valueStrArr[j]=conZi.getAttribute("value");
valueObjArr[j]=Utils.changeType(valueStrArr[j], fieldArr[j].getType());
argsTypes[j]=fieldArr[j].getType();
}
Constructor constructor=cla.getDeclaredConstructor(argsTypes);
constructor.setAccessible(true);
obj=constructor.newInstance(valueObjArr);
}
mapBeans.put(id, obj);
}
}
public void close(){
try {
in.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public Object getBean(String name){return mapBeans.get(name);}
}
14.5 测试
MyClassPathXmlApplicationContext context=
new MyClassPathXmlApplicationContext("com/zhiyou100/test01/spring_config.xml");
Student s11=(Student)context.getBean("stu3");
System.out.println(s11);
context.close();
![在这里插入图片描述](https://img-blog.csdnimg.cn/db5a717db0754b948addf12b241f8ed6.png)