---
title: "反射与工厂"
author: "i_coding"
date: "2011-05-19T16:19:24Z"
tags: []
categories: ["企业架构"]
slug: "untitled"
draft: false
lastmod: "2011-05-19T16:19:24Z"
id: 1050436
url: "https://i-coding.iteye.com/blog/1050436"
---

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