1. 程式人生 > >dom4j簡單實現IoC

dom4j簡單實現IoC

IoC:控制反轉(Inversion of Control,縮寫為IoC),是面向物件程式設計中的一種設計原則,可以用來減低計算機程式碼之間的耦合度。其中最常見的方式叫做依賴注入(Dependency Injection,簡稱DI),還有一種方式叫“依賴查詢”(Dependency Lookup)。通過控制反轉,物件在被建立的時候,由一個調控系統內所有物件的外界實體,將其所依賴的物件的引用傳遞給它。也可以說,依賴被注入到物件中。

 

dom4j用來解析xml檔案,得到bean標籤的id屬性和class屬性後,就可以用反射來建立物件。

目錄結構:

            

配置檔案:

<?xml version="1.0" encoding="UTF-8"?>

<beans>
    <bean id="testBean" class="cn.zjm.frame.spring.bean.TestBean"></bean>
</beans>

Bean:

package cn.zjm.frame.spring.bean;

public class TestBean {
    public TestBean() {
        System.out.println("TestBean被建立");
    }

    public void sayHello() {
        System.out.println("testBean : hello");
    }
}

IoC實現:

package cn.zjm.frame.spring.ioc;

import cn.zjm.frame.spring.bean.TestBean;
import org.dom4j.*;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class LoadXMLConfigFile {

    private static final String DEFAULT_PATH = Object.class.getClass().getResource("/").getPath();

    private static final String DEFAULT_CONFIGURE_NAME = "config.xml";

    private Map<String, String> beanMap = new HashMap<String, String>();

    private Document document;

    public LoadXMLConfigFile() {
        this(DEFAULT_PATH, DEFAULT_CONFIGURE_NAME);
    }

    public LoadXMLConfigFile(String fileName) {
        this(DEFAULT_PATH, fileName);
    }

    public LoadXMLConfigFile(String path, String fileName) {
        init(path, fileName);
    }

    private void init(String path, String fileName) {
        document = loadFile(path, fileName);
        String beanName;
        String beanClass;
        Element root = document.getRootElement();
        Element bean;
        Iterator beans = root.elementIterator("bean");
        if(beans == null) return;
        while (beans.hasNext()) {
            bean = (Element) beans.next();
            beanName = bean.attribute("id").getText();
            beanClass = bean.attribute("class").getText();
            beanMap.put(beanName, beanClass);
        }
    }

    public Class getBean(String beanName) throws ClassNotFoundException {
        if (beanMap.containsKey(beanName)) {
            String classPath = beanMap.get(beanName);
            return Class.forName(classPath);
        } else {
            throw new ClassNotFoundException();
        }
    }

    private Document loadFile(String path, String fileName) {
        try {
            Document d;
            File file = new File(path + fileName);
            SAXReader reader = new SAXReader();
            d = reader.read(file);
            return d;
        } catch (DocumentException e) {
            System.out.println("沒有找到配置檔案");
            throw new RuntimeException(e);
        }
    }

    public Document getDocument() {
        return document;
    }
}

測試方法:

public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        LoadXMLConfigFile conf = new LoadXMLConfigFile("file.xml");
        Class testBean = conf.getBean("testBean");
        TestBean o = (TestBean) testBean.newInstance();
        o.sayHello();
    }

輸出結果:

TestBean被建立
testBean : hello

程式碼很簡單,就是讀取xml檔案內容,不同的功能讀取不同的標籤,得到標籤屬性和內容後,執行相應的操作。

Spring通過配置檔案,得到相應bean的全路徑,然後通過反射建立物件,基本原理就是這樣。dom4j IoC