模拟Spring的Ioc

用这样一个简单的场景模拟Spring的Ioc:



先建立car类:

package com.deciphering.car;

public interface Car {

	public String getBrand();

	public void run();
}

package com.deciphering.carImplementation;

import com.deciphering.car.Car;

public class BMWCar implements Car {

	private final String MyBrand = "宝马";

	@Override
	public String getBrand() {
		return MyBrand;
	}

	@Override
	public void run() {
		System.out.println(MyBrand + "  is runnning!");
	}
}

再建立human类

package com.deciphering.human;

import com.deciphering.car.Car;

public class Human {

	private Car car;

	public Car getCar() {
		return car;
	}

	public void setCar(Car car) {
		this.car = car;
	}

	public void myCarRun() {
		car.run();
	}

}

很简单,就是2个javabean;

接下来模拟bean工厂

package com.deciphering.spring;

public interface BeanFactoryTest {

	public Object getBean(String id);
}

package com.deciphering.spring;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

public class ClassPathXmlApplicationContextTest implements BeanFactoryTest {

	private final Map<String, Object> beans = new HashMap<String, Object>();

	public ClassPathXmlApplicationContextTest() throws Exception {
		final SAXBuilder sb = new SAXBuilder();

		System.out.println("开始读取配置文件");
		final Document doc = sb.build(this.getClass().getClassLoader().getResourceAsStream("beans.xml"));
		final Element root = doc.getRootElement();
		final List list = root.getChildren("bean");
		for (int i = 0; i < list.size(); i++) {
			final Element element = (Element) list.get(i);
			final String id = element.getAttributeValue("id");
			final String clazz = element.getAttributeValue("class");
			final Object o = Class.forName(clazz).newInstance();
			System.out.println("bean id: " + id);
			System.out.println("bean class: " + clazz);
			beans.put(id, o);

			for (final Element propertyElement : (List<Element>) element.getChildren("property")) {
				final String name = propertyElement.getAttributeValue("name");
				final String bean = propertyElement.getAttributeValue("bean");
				System.out.println("property name: " + name);
				System.out.println("property bean: " + bean);
				final Object beanObject = beans.get(bean);

				final String methodName = "set" + name.substring(0, 1).toUpperCase() + name.substring(1);
				System.out.println("根据property name构造method name = " + methodName);

				System.out.println("开始利用反射执行setter注入... ");
				final Method m = o.getClass().getMethod(methodName, beanObject.getClass().getInterfaces()[0]);
				m.invoke(o, beanObject);
			}
		}
	}

	@Override
	public Object getBean(String id) {
		return beans.get(id);
	}

}

然后建立一个配置文件beans.xml

<beans>

	<bean id="car" class="com.deciphering.carImplementation.BMWCar" >
	</bean>
	
	<bean id="human" class="com.deciphering.human.Human" >
		<property name = "car" bean = "car"></property>
	</bean>		
	
</beans>

接下来就写一个测试类:

package com.deciphering.humen;

import org.junit.Test;

import com.deciphering.human.Human;
import com.deciphering.spring.ClassPathXmlApplicationContextTest;

public class HumenTest {

	@Test
	public void testHumen() throws Exception {

		final ClassPathXmlApplicationContextTest ctx = new ClassPathXmlApplicationContextTest();

		final Human human = (Human) ctx.getBean("human");

		// Car car = (BMWCar)ctx.getBean("car");

		// human.setCar(car);
		System.out.println("setter注入完成,开始执行测试 ");
		human.myCarRun();
	}

}

输出:

开始读取配置文件
bean id: car
bean class: com.deciphering.carImplementation.BMWCar
bean id: human
bean class: com.deciphering.human.Human
property name: car
property bean: car
根据property name构造method name = setCar
开始利用反射执行setter注入... 
setter注入完成,开始执行测试 
宝马  is runnning!


一个简单的Spring的Ioc就完成了



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值