最近閒來無事研究了一下IOC底層,因而本身寫了一段代碼模擬一下spring是如何作依賴注入的。 java
寫框架首先要懂反射,JAVA反射機制是在運行狀態中,對於任意一個類,都可以知道這個類的全部屬性和方法;對於任意一個對象,都可以調用它的任意一個方法和屬性;這種動態獲取的信息以及動態調用對象的方法的功能稱爲java語言的反射機制。 spring
主要思想以下: 框架
注意:事先定義兩個類,一個用於描述bean的id和class以及它的properties,一個用於描述bean屬性property的name和ref。 dom
propertyDefinition類表示的是描述引用對象的對象,BeanDefinition描述的是建立bean實例的對象。 this
1、bean的實例化:經過sax解析spring.xml文件獲取注入bean的id和class而後存放到BeanDefinition對象中去,將BeanDefinition類放到list集合中去,而後建立map集合,遍歷list集合獲取BeanDefinition中的id和利用反射機制將className實例化後的對象存放到map集合中,最後再用戶建立該上下文的類的時候,在構造方法中加載實例化bean的代碼,最後寫個經過id獲取bean的公共方法。 spa
二,bean對象屬性的注入:bean屬性的注入稍顯複雜,已知經過步驟一咱們能夠拿到BeanDefinition實例化對象。下面開始分析bean對象屬性注入的邏輯。 .net
第一步:遍歷集合中全部的bean對象。 code
第二步:經過bean的class對象獲取封裝bean信息的BeanInfo對象,經過BeanInfo對象獲取該bean的屬性描述的集合ps。 xml
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 List<BeanDefinition> beanDefines = new ArrayList<BeanDefinition>(); private Map<String, Object> sigletons = new HashMap<String, Object>(); 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<String,String> nsMap = new HashMap<String,String>(); nsMap.put("ns","http://www.springframework.org/schema/beans");//加入命名空間 XPath xsub = document.createXPath("//ns:beans/ns:bean");//建立beans/bean查詢路徑 xsub.setNamespaceURIs(nsMap);//設置命名空間 List<Element> 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<Element> 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); } }