您的位置:首页 > 编程语言 > Java开发

spring DI/IOC核心模拟程序

2016-01-22 16:05 507 查看

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();
}
}

执行效果:




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: