2007-10-29
Spring 对单态和工厂模式的实现
关键字: springSpring使用配置文件管理所有的bean,该bean就是Spring工厂能产生的全部实例。下面是配置文件:
xml 代码
- <beans>
- <bean id="chinase" class="lee.Chinese" />
- <bean id="american" class="lee.American" />
- beans>
主程序部分如下:
java 代码
- public class SpringTest
- { public static void main(String[] args)
- {
- 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"));
- }
- }
使用Spring至少有一个好处:即使没有工厂类,程序一样可以使用工厂模式。
java 代码
- public class SpringTest
- {
- public static void main(String[] args)
- {
- ApplicationContext ctx = new FileSystemXmlApplicationContext("bean.xml");
- Person p1 = null ;
- p1 = (Person)ctx.getBean("chinese");
- Person p2 = null ;
- p2 = (Person)ctx.getBean("chinese");
- System.out.println(p1==p2);
- }
- }
程序执行结果是: true
表明 Spring 对接收容器管理的全部bean,默认采用单态模式管理。性能上,单态的bean比非单态的bean更优秀。
发表评论
- 浏览: 173107 次
- 性别:

- 来自: 烟台

- 详细资料
搜索本博客
我的相册
200804090708271400855637
共 39 张
共 39 张
最近加入圈子
最新评论
-
2007,年终总结
qingzi2534 写道08年一开始计划就完全没有执行,早上起不来,没有时间吃 ...
-- by 刑天战士 -
2007,年终总结
qingzi2534 写道08年一开始计划就完全没有执行,早上起不来,没有时间吃 ...
-- by weiqingfei -
2007,年终总结
08年一开始计划就完全没有执行,早上起不来,没有时间吃早餐,晚上连续熬夜到12点 ...
-- by qingzi2534 -
2007,年终总结
曾经早上8点干起然后干到转钟一直干了半年,最后身体几乎搞垮的人飘过。。。
-- by zhangfeiyu2005 -
2007,年终总结
qingzi2534 写道 每天都要在2点前睡觉 很好奇,到底是几点上班的 ...
-- by fqtrnt






评论排行榜