最近闲来无事研究了一下IOC底层,于是自己写了一段代码模拟一下spring是如何做依赖注入的。
写框架首先要懂反射,JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法和属性;这种动态获取的信息以及动态调用对象的方法的功能称为java语言的反射机制。
主要思想如下:
注意:事先定义两个类,一个用于描述bean的id和class以及它的properties,一个用于描述bean属性property的name和ref。
propertyDefinition类表示的是描述引用对象的对象,BeanDefinition描述的是创建bean实例的对象。
一、bean的实例化:通过sax解析spring.xml文件获取注入bean的id和class然后存放到BeanDefinition对象中去,将BeanDefinition类放到list集合中去,然后创建map集合,遍历list集合获取BeanDefinition中的id和利用反射机制将className实例化后的对象存放到map集合中,最后再用户创建该上下文的类的时候,在构造方法中加载实例化bean的代码,最后写个通过id获取bean的公共方法。
二,bean对象属性的注入:bean属性的注入稍显复杂,已知通过步骤一我们可以拿到BeanDefinition实例化对象。下面开始分析bean对象属性注入的逻辑。
第一步:遍历集合中所有的bean对象。
第二步:通过bean的class对象获取封装bean信息的BeanInfo对象,通过BeanInfo对象获取该bean的属性描述的集合ps。
PropertyDescriptor[] ps = Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors();
第三步:获取bean的properties属性,遍历propertyDefinition集合,获取propertyDefinition对象,然后再嵌套循环遍历ps,然后判断propertyDefinition对象的name属性和beanInfo对象中关于bean的描述的className是否相等,如果相等说明ref的就是该bean,然后给该bean的实例化对象,写入到当前属性对对应的bean的set方法中。
Method setter = properdesc.getWriteMethod();//获取属性的set方法if(setter!=null){ Object value = sigletons.get(propertyDefinition.getRef());//如果set私有,强制访问
setter.setAccessible(true);
setter.invoke(bean, value);//把引用对象注入到属性}第四步:将 bean对象属性的注入实例化方法加载到构造方法当中。
下面就利用反射写一段伪代码大概描述一下spring是如何实现IOC的。
package junit.test;import java.beans.IntrospectionException;import java.beans.Introspector;import java.beans.PropertyDescriptor;import java.lang.reflect.Method;import java.net.URL;import java.util.ArrayList;import java.util.HashMap;import java.util.HashSet;import java.util.List;import java.util.Map;import org.dom4j.Document;import org.dom4j.Element;import org.dom4j.XPath;import org.dom4j.io.SAXReader;public class MyClassPathXMLApplicationContext { private ListbeanDefines = new ArrayList (); private Map sigletons = new HashMap (); public MyClassPathXMLApplicationContext(String filename){ this.readXML(filename); this.instanceBeans(); this.injectObject(); } /** * 为bean对象的属性注入值 */ private void injectObject() { for(BeanDefinition beanDefinition : beanDefines){ Object bean = sigletons.get(beanDefinition.getId()); if(bean!=null){ try { PropertyDescriptor[] ps = Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors(); for(PropertyDefinition propertyDefinition : beanDefinition.getPropertys()){ for(PropertyDescriptor properdesc : ps){ if(propertyDefinition.getName().equals(properdesc.getName())){ Method setter = properdesc.getWriteMethod();//获取属性的setter方法 ,private if(setter!=null){ Object value = sigletons.get(propertyDefinition.getRef()); setter.setAccessible(true); setter.invoke(bean, value);//把引用对象注入到属性 } break; } } } } catch (Exception e) { } } } } /** * 完成bean的实例化 */ private void instanceBeans() { for(BeanDefinition beanDefinition : beanDefines){ try { if(beanDefinition.getClassName()!=null && !"".equals(beanDefinition.getClassName().trim())) sigletons.put(beanDefinition.getId(), Class.forName(beanDefinition.getClassName()).newInstance()); } catch (Exception e) { e.printStackTrace(); } } } /** * 读取xml配置文件 * @param filename */ private void readXML(String filename) { SAXReader saxReader = new SAXReader(); Document document=null; try{ URL xmlpath = this.getClass().getClassLoader().getResource(filename); document = saxReader.read(xmlpath); Map nsMap = new HashMap (); nsMap.put("ns","http://www.springframework.org/schema/beans");//加入命名空间 XPath xsub = document.createXPath("//ns:beans/ns:bean");//创建beans/bean查询路径 xsub.setNamespaceURIs(nsMap);//设置命名空间 List beans = xsub.selectNodes(document);//获取文档下所有bean节点 for(Element element: beans){ String id = element.attributeValue("id");//获取id属性值 String clazz = element.attributeValue("class"); //获取class属性值 BeanDefinition beanDefine = new BeanDefinition(id, clazz); XPath propertysub = element.createXPath("ns:property"); propertysub.setNamespaceURIs(nsMap);//设置命名空间 List propertys = propertysub.selectNodes(element); for(Element property : propertys){ String propertyName = property.attributeValue("name"); String propertyref = property.attributeValue("ref"); PropertyDefinition propertyDefinition = new PropertyDefinition(propertyName, propertyref); beanDefine.getPropertys().add(propertyDefinition); } beanDefines.add(beanDefine); } }catch(Exception e){ e.printStackTrace(); } } /** * 获取bean实例 * @param beanName * @return */ public Object getBean(String beanName){ return this.sigletons.get(beanName); }}