Spring IOC底层原理
程序员文章站
2024-01-11 10:02:52
Spring IOC底层原理读取配置文件,解析XML通过反射机制实例化配置文件中配置的所有Bean1.自定义IOC容器类通过运行时ID来获取Beanpackage com.myt.ioc;import org.dom4j.Attribute;import org.dom4j.Document;import org.dom4j.DocumentException;import org.dom4j.Element;import org.dom4j.io.SAXReader;i...
Spring IOC底层原理
package com.myt.ioc;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.xml.sax.SAXException;
import sun.print.PeekGraphics;
import javax.sound.midi.Soundbank;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* @program: spring_code
* @description:
* @author: Ma YaTing
* @create: 2020-11-23 20:12
*/
public class ClassPathXmlApplicationContext implements ApplicationContext{
//IOC容器
private Map<String,Object> ioc = new HashMap<String, Object>();
public ClassPathXmlApplicationContext(String path){
//解析xml
try {
SAXReader reader = new SAXReader();
//读取配置文件
Document document = reader.read("./springioc/src/main/resources/"+path);
//获取根节点 beans
Element rootElement = document.getRootElement();
//获取Iterator迭代器 属于beans
Iterator<Element> iterator = rootElement.elementIterator();
while (iterator.hasNext()){
//2个 bean
Element element = iterator.next();
//取出key
String id = element.attributeValue("id");
String className = element.attributeValue("class");
//System.out.println(id);
//System.out.println(className);
//通过反射机制创建对象
//获取反射的源头 Class 这里是全类名
Class clazz = Class.forName(className);
//获取无参构造函数 未传参数即无参构造 创建目标对象
Constructor constructor = clazz.getConstructor();
Object object = constructor.newInstance();
//给目标对象赋值
//获取迭代器 属于bean
Iterator<Element> beanIterator = element.elementIterator();
while (beanIterator.hasNext()){
Element property = beanIterator.next();
String name = property.attributeValue("name");
String valueStr = property.attributeValue("value");
String ref = property.attributeValue("ref");
if(ref == null){//普通赋值
//获取set方法
String methodName = "set" + name.substring(0,1).toUpperCase() + name.substring(1);
Field field = clazz.getDeclaredField(name);
System.out.println(field.getType());
//公有方法 方法名 参数类型
Method method = clazz.getDeclaredMethod(methodName,field.getType());
//给方法赋值 对象 值
//根据成员遍历的数据类型 将 value 进行转换
Object value = null;
if (field.getType().getName() == "long") {
value = Long.parseLong(valueStr);
}
if(field.getType().getName() == "java.lang.String"){
value = valueStr;
}
if(field.getType().getName() == "int"){
value = Integer.parseInt(valueStr);
}
method.invoke(object,value);
}else {//依赖注入
}
ioc.put(id,object);
}
}
System.out.println(ioc);
} catch (DocumentException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
}
@Override
public Object getBean(String id) {
return ioc.get(id);
}
}
ApplicationContext applicationContext = new
ClassPathXmlApplicationContext("spring.xml");
Student student = (Student)applicationContext.getBean(Student.class);
System.out.println(student);
这种方式存在一个问题:
配置文件中一个数据类型的对象只能有一个实例,否则会抛出异常,因为没有唯一的Bean
本文地址:https://blog.csdn.net/weixin_43273005/article/details/110184061