国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

spring依賴注入原理剖析

2019-11-15 00:19:25
字體:
來源:轉載
供稿:網友
sPRing依賴注入原理剖析

PropertyDefinition.java

 1 package junit.test; 2  3 public class PropertyDefinition { 4     private String name; 5     private String ref; 6      7     public PropertyDefinition(String name, String ref) { 8         this.name = name; 9         this.ref = ref;10     }11     12     public String getName() {13         return name;14     }15     public void setName(String name) {16         this.name = name;17     }18     public String getRef() {19         return ref;20     }21     public void setRef(String ref) {22         this.ref = ref;23     }24     25 }
PropertyDefinition

BeanDefinition.java

 1 package junit.test; 2  3 import java.util.ArrayList; 4 import java.util.List; 5  6 public class BeanDefinition { 7     private String id; 8     private String className; 9     private List<PropertyDefinition> propertys = new ArrayList<PropertyDefinition>();10     11     public BeanDefinition(String id, String className) {12         this.id = id;13         this.className = className;14     }15     public String getId() {16         return id;17     }18     public void setId(String id) {19         this.id = id;20     }21     public String getClassName() {22         return className;23     }24     public void setClassName(String className) {25         this.className = className;26     }27     public List<PropertyDefinition> getPropertys() {28         return propertys;29     }30     public void setPropertys(List<PropertyDefinition> propertys) {31         this.propertys = propertys;32     }33     34 }
BeanDefinition

ItcastClassPathxmlapplicationContext.java

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 a {    private List<BeanDefinition> beanDefines = new ArrayList<BeanDefinition>();    private Map<String, Object> sigletons = new HashMap<String, Object>();        public ItcastClassPathXMLApplicationContext(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("http://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);    }}
ItcastClassPathXMLApplicationContext


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 丰宁| 屏山县| 灵川县| 建阳市| 肥东县| 玛多县| 涿鹿县| 金华市| 张家川| 郎溪县| 东乌珠穆沁旗| 梨树县| 三门峡市| 渝中区| 蒲江县| 莲花县| 民和| 奉新县| 彭水| 邓州市| 达孜县| 巴南区| 江口县| 三明市| 鲁山县| 定陶县| 惠安县| 黄浦区| 安陆市| 松原市| 萨嘎县| 潼南县| 永吉县| 彭山县| 武城县| 杨浦区| 利辛县| 新化县| 台南县| 广西| 元谋县|