侧边栏壁纸
博主头像
王小木人

这是很长,很好的一生

  • 累计撰写 141 篇文章
  • 累计创建 43 个标签
  • 累计收到 7 条评论

目 录CONTENT

文章目录

java 对象工厂模式读取配置文件创建对象

王小木人
2021-05-26 / 0 评论 / 0 点赞 / 1,079 阅读 / 1,093 字
​
package com.market.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;

public class ObjectFactory {
	public static HashMap<String, Object> objects = new HashMap<String, Object>();
	static {
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				ObjectFactory.class.getClassLoader().getResourceAsStream(
						"Object")));
		String row = null;
		try {
			while ((row = reader.readLine()) != null) {
				try {
					objects.put(row.split("=")[0], Class.forName(row.split("=")[1]).newInstance());
				} catch (InstantiationException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}

​

1.png

0

评论区