csdnstudylp 2011-08-04
bean.xml
<!-- beans是Spring配置文件的根元素--> <beans> <bean id="chinese" class="lee.Chinese" /> <bean id="american" class="lee.American" /> </beans>
主程序(SpringTest)
public static void main(String[] args) { //实例化Spring容器 ApplicationContext ctx = new FileSystemXmlApplicationContext("bean.xml"); Person p= null; p=(person)ctx.getBean("chinese"); System.out.println(p.sayHello("wawa")); System.out.println(p.sayCoogbye("wawa")); p=(Person)ctx.getBean("american"); System.out.println(p.sayHello("wawa")); System.out.println(p.sayGoodBye("wawa")); }
从上可知,ApplicationContext就好比FactoryPerson类,故Spring,只需要接口,和实现接口的类,便产生了工厂,无需工厂类的实现。