Person接口:
package org.com.spring.ioc.test;
interface Person {
 void sayHello();
}
Student类:
package org.com.spring.ioc.test;
public class Student implements Person {
 @Override
 public void sayHello() {
 // TODO Auto-generated method stub
 System.out.println("Student implements Person say hello");
 }
}
Teacher类:
package org.com.spring.ioc.test;
public class Teacher implements Person {
 @Override
 public void sayHello() {
 // TODO Auto-generated method stub
 System.out.println("Teacher implements Person say hello");
 }
}
Test类:
package org.com.spring.ioc.test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
 public class Test {
 private Person dao;
 public Person getDao() {
 return dao;
 }
 public void setDao(Person dao) {
 this.dao = dao;
 }
 public void testPerson() {
 dao.sayHello();
 }
 public static void main(String[] args) {
 /* ---------Spring Ioc具体例子--------- */
 // 获取bean工厂
 BeanFactory beanfactory = new XmlBeanFactory(new FileSystemResource(
 "src/org/com/spring/ioc/test/applicationContext.xml"));
 // 获取在配置文件中跟testDAO对应的类的实例
 Test test = (Test) beanfactory.getBean("test");
 // 此处test.testPerson();他所调用的是Student的还是Teacher的方法是由beanfactory解析后传进来的
 //好莱坞原则中的"你不要动,我去找你","我"指具体的实现方法,是Student的sayHello还是Teacher的sayHello,
 //"你"指test.testPerson,testPerson调用sayHello方法,spring解析传入sayHello方法的来源(Student或Teacher))
 test.testPerson();
 }
 }
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:p="http://www.springframework.org/schema/p"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 <bean id="student" class="org.com.spring.ioc.test.Student"></bean>
 <bean id="teacher" class="org.com.spring.ioc.test.Teacher"></bean>
 <bean id="test" class="org.com.spring.ioc.test.Test">
 <property name="dao" ref="student"></property>
 </bean>
</beans>