Spring的IOC机制,利用了java的反射机制,通过读取配置文件,动态生成所需要的类。
public static void main(String[] args) { // TODO Auto-generated method stub Resource rs = new FileSystemResource("helloMessage.xml"); BeanFactory bf = new XmlBeanFactory(rs); Person person = (Person)bf.getBean("person"); String s = person.sayHello(); System.out.println("The person is currently saying " + s); }
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="helloWorld" class="com.zkzk945.spring.chapter01.HelloWorld"></bean> <bean id="helloChina" class="com.zkzk945.spring.chapter01.HelloChina"></bean> <bean id="person" class="com.zkzk945.spring.chapter01.Person"> <property name="helloMessage" ref="helloWorld"/> </bean> </beans>
源码
https://git.oschina.net/zkzk945/Spring.git