Spring原理自我实践之IOC的模拟实现
2009-09-18 00:00:00 来源:WEB开发网Class_4:测试类
Java代码
package test;
import com.java.interfaces.Car;
import com.java.interfaces.Wheel;
import com.java.mySpring.MyIOC;
public class TestMyIOC {
public static void main(String args[]) throws Exception {
MyIOC myIOC = new MyIOC();
Car car1 = (Car) myIOC.getBean("carImpl");
car1.setColor("green");
Wheel wheel = (Wheel) myIOC.getBean("wheelImpl");
System.out.println("对象的信息:"+car1.getId() + " " + car1.getColor() + " "
+ car1.getWheel().getId() + " " + car1.getWheel().getSize());
System.out.println("wheel.hashCode()" + wheel.hashCode());
System.out.println("结束");
}
}
配置文件:
Xml代码
<?xml version="1.0" encoding="UTF-8"?>
<beans>
<bean id="carImpl" class="com.java.beans.CarImpl">
<property name="id" value="001" />
<property name="color" value="red" />
<property name="wheel" ref="wheelImpl"/>
</bean>
<bean id="wheelImpl" class="com.java.beans.WheelImpl">
<property name="id" value="001" />
<property name="size" value="50cm" />
</bean>
</beans>
更多精彩
赞助商链接