当前位置: 代码迷 >> Web前端 >> spring对工场模式的实现
  详细解决方案

spring对工场模式的实现

热度:202   发布时间:2012-09-07 10:38:15.0
spring对工厂模式的实现
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,只需要接口,和实现接口的类,便产生了工厂,无需工厂类的实现。
  相关解决方案