测试之路,与你同行!
几种创建接口对象的方法
上一篇 /
下一篇 2010-04-08 20:49:21
/ 个人分类:spring
1.直接用new
类似: Parent son = new Son();
2.用
java的反射机制
public class PersonFactory{
public static Person getPersonInstence(String str){
if("man".equal(str)){
return new Man();
}
if("woman".equal(str)){
return new Woman();
}
}
3.使用Spring的DI
在AplicationContext.xml中配置bean的id,class
在应用中直接调getBean(id)
获得上下文:
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
通过上下文来获得实体对象的应用
Man man = (Man)context.getBean("man");
收藏
举报
TAG: