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 

猜你喜欢

转载自blog.csdn.net/u010960184/article/details/82877915