簡單類比SpringIOC工作機制流程開發,springioc機制

來源:互聯網
上載者:User

簡單類比SpringIOC工作機制流程開發,springioc機制

  緊接上一篇文章《輕鬆瞭解Spring中的控制反轉和依賴注入》講解了SpringIOC和DI的基本概念,這篇文章我們類比一下SpringIOC的工作機制,使我們更加深刻的理解其中的工作。再上代碼之前我們先來看看幾個類的設計目的。 

  BeanFactor介面:在Spring源碼中的定義是:持有對一定數量的Bean的定義,同時每個Bean都被唯一標識的對象(類),需要實現這個介面。根據對Bean的定義,該工廠將會返回一個包含Bean定義的對象的獨立執行個體(原型設計模式),或者單例共用(一個不錯的單例設計模式,)範圍是整個工廠的範圍(也可以理解成是整個容器下),返回哪種類型的執行個體依賴於Bean工廠的配置:API是相同的。因為Spring2.0中擴大了依賴範圍,可以根據具體應用上下問(如在Web環境中的請求和會話),BeanFactory是應用程式組件的中央註冊中心和集中配置。簡單的來說該介面定義了擷取Bean的方法,由子類去實現。

  BeanFactory介面:是實現BeanFactory介面的抽象基類。實現擷取Bean定義的方法。

  XmlContext類:繼承了AbstractBeanFactory抽象類別,進行Bean的註冊和註冊XML的讀取器。

  BeanInfo類:進行儲存Bean的資訊。

  接下來上代碼,需要注釋的地方我都添加上去了,類的目錄如下。

    

  類之間的結構圖如下

   以下是代碼

  

package ioc.factory;/** * Created by zzf on 2016/10/26. */public interface BeanFactory {    /**     * 根據對象的ID標識擷取對象執行個體     * @param name     * @return     */    Object getBean(String name);}
package ioc.factory;import ioc.info.BeanInfo;import ioc.reader.SourceReader;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.util.Map;/** * Created by zzf on 2016/10/26. * * 最頂層的IOC實現 * 該類負責從註冊器中取出註冊對象 * 實現從對象描述資訊轉換為對象執行個體的過程 * 實現根據名稱擷取對象的方法 * */public abstract class AbstractBeanFactory implements BeanFactory {    private  String filePath;    private Map<String,BeanInfo> container;    protected SourceReader reader;    public AbstractBeanFactory(String filePath){        this.filePath=filePath;        setReader(reader);    }    /**     * 由子類決定如果使用什麼樣的註冊讀取器     * 這裡使用了模板方法,父類定義抽象方法,但交給子類自由去設計方法內容     * @param reader     */    protected abstract void setReader(SourceReader reader);    //註冊bean    public void registerBeans(){        this.container=this.reader.loadBeans(filePath);    }    @Override    public Object getBean(String name) {        BeanInfo beanInfo=this.container.get(name);        if(beanInfo==null){            return null;        }else {            return this.parseBean(beanInfo);        }    }    /**     * 解析產生並產生對象執行個體     * 主要通過反射完成     * 根據類名,載入指定類,並取得該類的Class對象     * 使用Class對象執行個體化該類,擷取一個對象。     * 逐個設定對象欄位的值,這裡採用setter Method方式     *     * @param beanInfo 指定對象的描述資訊     * @return     */    protected  Object parseBean(BeanInfo beanInfo){        Class clazz;        try {            //載入Bean的執行個體            clazz=Class.forName(beanInfo.getType());            Object bean=clazz.newInstance();                        //擷取該對象下的所有方法,包括私人            Method[] methods=clazz.getDeclaredMethods();                        for (String property:beanInfo.getProperties().keySet()){                String setter="set"+ firstCharToUp(property);                for(Method method:methods){                    String methodName=method.getName();                                        if(methodName.equals(setter)){                        Object value=beanInfo.getProperties().get(property);                        //使用反射調用set方法                        method.invoke(bean,value);                    }                }            }            return bean;        } catch (ClassNotFoundException e) {            e.printStackTrace();        } catch (InstantiationException e) {            e.printStackTrace();        } catch (IllegalAccessException e) {            e.printStackTrace();        } catch (InvocationTargetException e) {            e.printStackTrace();        }            return null;    }    private String firstCharToUp(String property) {        System.out.println(property);        char [] c=property.toCharArray();        String first=String.valueOf(c[0]).toUpperCase();        c[0]=first.charAt(0);        System.out.println(String.valueOf(c));        return String.valueOf(c);    }}  package ioc.context;import ioc.factory.AbstractBeanFactory;import ioc.reader.SourceReader;import ioc.reader.XMLSourceReader;/** * Created by zzf on 2016/10/26. * 內容相關的構造方法 * 該方法中指明註冊讀取器(這裡採用的XML,讀者可以根據興趣去實現另外的方式如註解) * 並在構造該方法時一次性載入註冊的對象 */public class XMLContext extends AbstractBeanFactory{    public XMLContext(String filePath){        super(filePath);        this.setReader(new XMLSourceReader());        super.registerBeans();    }    @Override    protected void setReader(SourceReader reader) {        super.reader=reader;    }}

  

package ioc.reader;import ioc.info.BeanInfo;import java.util.Map;/** * Created by zzf on 2016/10/26. * 註冊讀取器介面 * 負責讀取使用者註冊的對象 * 繼承該介面的類可以實現多種讀取方式,如從設定檔中讀取,根據標註讀取,從網路中讀取等等 */public interface SourceReader {    Map<String,BeanInfo> loadBeans(String filePath);}

 

  

package ioc.context;import ioc.factory.AbstractBeanFactory;import ioc.reader.SourceReader;import ioc.reader.XMLSourceReader;/** * Created by zzf on 2016/10/26. * 內容相關的構造方法 * 該方法中指明註冊讀取器(這裡採用的XML,讀者可以根據興趣去實現另外的方式如註解) * 並在構造該方法時一次性載入註冊的對象 */public class XMLContext extends AbstractBeanFactory{    public XMLContext(String filePath){        super(filePath);        this.setReader(new XMLSourceReader());        super.registerBeans();    }    @Override    protected void setReader(SourceReader reader) {        super.reader=reader;    }}package ioc.info;import java.lang.Object;import java.util.HashMap;import java.util.Map;/** * Created by zzf on 2016/10/26. */public class BeanInfo {    private String id;    private String type;    private Map<String,Object> properties=new HashMap<String,Object>();    public String getId() {        return id;    }    public void setId(String id) {        this.id = id;    }    public String getType() {        return type;    }    public void setType(String type) {        this.type = type;    }    public Map<String, Object> getProperties() {        return properties;    }    public void setProperties(Map<String, Object> properties) {        this.properties = properties;    }    public void addProperty(String name, Object object)    {        this.properties.put(name, object);    }}   package ioc.reader;import ioc.info.BeanInfo;import org.dom4j.Attribute;import org.dom4j.Document;import org.dom4j.DocumentException;import org.dom4j.Element;import org.dom4j.io.SAXReader;import java.beans.IntrospectionException;import java.beans.Introspector;import java.beans.PropertyDescriptor;import java.io.InputStream;import java.io.Reader;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.util.HashMap;import java.util.Iterator;import java.util.Map;/** * Created by zzf on 2016/10/26. * 使用 dom4j進行Xml的讀取操作 */public class XMLSourceReader implements SourceReader {    @Override    public Map<String, BeanInfo> loadBeans(String filePath) {        //讀取指定的設定檔        SAXReader reader = new SAXReader();        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();        //從class目錄下擷取指定的xml檔案        InputStream ins = classLoader.getResourceAsStream(filePath);        Document doc = null;        try {            doc = reader.read(ins);        } catch (DocumentException e) {            e.printStackTrace();        }        //獲得根節點        Element root = doc.getRootElement();        Map<String,BeanInfo>beanInfoMap=new HashMap<String, BeanInfo>();        //遍曆bean        for (Iterator i = root.elementIterator("bean"); i.hasNext();){            Element element = (Element) i.next();            //擷取bean的屬性id和class            Attribute id = element.attribute("id");            Attribute cls = element.attribute("class");            try {                //利用Java反射機制,通過class的名稱擷取Class對象                Class bean=Class.forName(cls.getText());                //擷取對應class的資訊                java.beans.BeanInfo info= Introspector.getBeanInfo(bean);                //擷取其屬性描述                PropertyDescriptor [] propertyDescriptors=info.getPropertyDescriptors();                Method method;                Object object=bean.newInstance();                BeanInfo beanInfo=new BeanInfo();                for(Iterator iterator=element.elementIterator("property");iterator.hasNext();){                    Element foo2= (Element) iterator.next();                    //擷取該property的name屬性                    Attribute name = foo2.attribute("name");                    String value = null;                    //擷取該property的子項目value的值                    for(Iterator ite1 = foo2.elementIterator("value"); ite1.hasNext();) {                        Element node = (Element) ite1.next();                        value = node.getText();                        break;                    }                    System.out.println("name:"+name.getText()+"value"+value);                for (int j=0;j<propertyDescriptors.length;j++){                    if(propertyDescriptors[j].getName().equalsIgnoreCase(name.getText())){                        method=propertyDescriptors[j].getWriteMethod();                        //利用Java的反射極致調用對象的某個set方法,並將值設定進去                        method.invoke(object,value);                        //將擷取的對象屬性資訊存入我們自訂的BeanInfo當中                        beanInfo.addProperty(name.getText(),value);                }                }                    beanInfo.setId(id.getText());                    beanInfo.setType(cls.getText());                    beanInfoMap.put(id.getText(),beanInfo);                }                return beanInfoMap;            } catch (ClassNotFoundException e) {                e.printStackTrace();            } catch (IntrospectionException e) {                e.printStackTrace();            } catch (InstantiationException e) {                e.printStackTrace();            } catch (IllegalAccessException e) {                e.printStackTrace();            } catch (InvocationTargetException e) {                e.printStackTrace();            }        }        return null;    }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.