首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

反照与工厂

2012-08-27 
反射与工厂代码测试public class BeanFactory {private Properties props new Properties()public Bean

反射与工厂
代码测试
public class BeanFactory {
private Properties props = new Properties();

public BeanFactory(String path) {
try {
FileInputStream fin = new FileInputStream(path);
props.load(fin);
} catch (Exception e) {
e.printStackTrace();
}
}

@SuppressWarnings("unchecked")
public Object getBean(String beanName) {
String className = props.getProperty(beanName);
Object obj = null;
try {
Class c = Class.forName(className);
obj = c.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
return obj;
}

public static void main(String[] args) {
BeanFactory bf = new BeanFactory("F:\\config.xml");
bf.getBean("test");
}

热点排行