spring DI/IOC核心模拟程序

1 篇文章 0 订阅
1 篇文章 0 订阅

DI/IOC概述:

依赖注入/控制反转。某一个类中需要使用其他类中的方法来实现业务时,一般采用在前者中声明后者并实例化,然后用后者的实例在前者中调用后者的方法,这种情形下,前者依赖后者,被依赖类的对象生灭由依赖类控制,这种做法耦合度较高。在使用spring的情形下,通过反射机制,类统一在spring中注册,被依赖对象统一由spring注入到依赖对象中。依赖注入和控制反转是站在不同的角度对同一动作的不同描述,DI,被依赖对象由spring容器实例化并注入到依赖对象中,IOC,被依赖对象的生灭由依赖对象控制转换为spring容器。


案例:

有三个类,Iron(铁),Axe(斧子),HumanBeing(人类),人类类依赖斧子类,斧子类依赖铁类。

Iron:

package com.sunsharing.di;

/**
 * Created by baich on 2016/1/22.
 */
public class Iron {
    public void useage() {
        System.out.println("Iron used to make tools");
    }
}

Axe:
package com.sunsharing.di;

/**
 * Created by baich on 2016/1/22.
 */
public class Axe {
    private Iron iron;

    public Iron getIron() {
        return iron;
    }

    public void setIron(Iron iron) {
        this.iron = iron;
    }

    public void cut() {
        iron.useage();
        System.out.println("cut tree!");
    }
}

HumanBeing:
package com.sunsharing.di;

/**
 * Created by baich on 2016/1/22.
 */
public class HumanBeing {
    private Axe axe;

    public Axe getAxe() {
        return axe;
    }

    public void setAxe(Axe axe) {
        this.axe = axe;
    }

    public void useAxe() {
        axe.cut();
    }
}

配置文件bean.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans>
    <bean id="Iron" class="com.sunsharing.di.Iron"></bean>
    
    <bean id="Axe" class="com.sunsharing.di.Axe">
        <property name="Iron" ref="Iron"/>
    </bean>

    <bean id="HumanBeing" class="com.sunsharing.di.HumanBeing">
        <property name="Axe" ref="Axe"/>
    </bean>
</beans>

spring引擎SpringEngin:
package com.sunsharing.di;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.io.FileWriter;
import java.lang.reflect.Method;
import java.util.List;

/**
 * Created by baich on 2016/1/22.
 */

public class SpringEngin {

    /**
     * 递归获取对象
     * @param ele 父节点
     * @param className bean id值
     * @return
     */
    public Object getObj(Element ele, String className) throws Exception {
        List<Element> elements = ele.elements("bean");
        for (Element element : elements) {

            Attribute atb = element.attribute("id");
            String idV = atb.getValue();

            if(idV.equals(className)) {
                Attribute atb02 = element.attribute("class");
                String classV = atb02.getValue();

                Object obj = Class.forName(classV).newInstance();

                List<Element> eleList = element.elements("property");
                for (Element el : eleList) {
                    Attribute atb03 = el.attribute("name");
                    String nV = atb03.getValue();

                    Attribute atb04 = el.attribute("ref");
                    String refV = atb04.getValue();

                    Object ob = getObj(ele, refV);

                    Method mt = obj.getClass().getMethod("set" + nV, ob.getClass());
                    mt.invoke(obj, ob);
                }
                return obj;
            }
        }
        return  null;
    }
    
    /**
     * 
     * @param className 待实例化的类
     * @return
     * @throws Exception
     */
    public Object getObj(String className) throws Exception {
        SAXReader reader = new SAXReader();

        File file = new File("bean.xml");
        Document document = reader.read(file);
        Element root = document.getRootElement();

        Object obj = getObj(root, className);
        return obj;
    }

    public static void main(String[] args) throws Exception {
        SpringEngin se = new SpringEngin();
        HumanBeing hb = (HumanBeing) se.getObj("HumanBeing");
        hb.useAxe();
    }
}

执行效果:




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值