ITeye博客

反射与工厂

i_coding · 2011-05-19 16:19 · 企业架构 · Markdown

代码测试 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");
}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。