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,只需要接口,和实现接口的类,便产生了工厂,无需工厂类的实现。
相关推荐
yangyujun 2020-08-21
jkzyx 2020-04-17
缘起宇轩阁 2019-12-30
swimmer000 2019-12-30
王志龙 2019-12-26
gougouzhang 2019-12-13
徐建岗网络管理 2011-12-18
HuangXiaoChuan 2019-03-20
一个过客 2019-03-25
machao 2019-03-21
cyAspNet 2019-07-01
Triones 2019-07-01
HavenZhao 2019-07-01
chengrile 2014-10-26
programisaart 2019-06-28
fuziwang 2019-06-28