/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
* 对象池,根据配置文件信息来创建java对象。使用反射。
*
*/
package reflect;
import java.io.IOException;
import java.util.Map;
import java.util.HashMap;
import java.io.FileInputStream;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author hou
*/
public class ObjectPoolFactory {
//定义一个对象池。
private Map<String, Object> objectPool = new HashMap<String, Object>();
//创建对象,利用反射
private Object createObject(String clazzName) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
Class<?> clazz = Class.forName(clazzName);
return clazz.newInstance();
}
//初始化对象池
public void initPool(String fileName) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
FileInputStream fis = null;
try {
fis = new FileInputStream(fileName);
Properties props = new Properties();
props.load(fis);
for (String name : props.stringPropertyNames()) {
objectPool.put(name, createObject(props.getProperty(name)));
}
}
catch (IOException ex) {
Logger.getLogger(ObjectPoolFactory.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
if (fis != null) {
fis.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
//根据名字找对象
public Object getObject(String name){
return objectPool.get(name);
}
public static void main(String[] args) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
//a.properties
ObjectPoolFactory ob = new ObjectPoolFactory();
ob.initPool("D:/a.properties");
ob.getObject("a");
System.out.println(ob.getObject("a")+"=========");
}
}
* To change this template, choose Tools | Templates
* and open the template in the editor.
* 对象池,根据配置文件信息来创建java对象。使用反射。
*
*/
package reflect;
import java.io.IOException;
import java.util.Map;
import java.util.HashMap;
import java.io.FileInputStream;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author hou
*/
public class ObjectPoolFactory {
//定义一个对象池。
private Map<String, Object> objectPool = new HashMap<String, Object>();
//创建对象,利用反射
private Object createObject(String clazzName) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
Class<?> clazz = Class.forName(clazzName);
return clazz.newInstance();
}
//初始化对象池
public void initPool(String fileName) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
FileInputStream fis = null;
try {
fis = new FileInputStream(fileName);
Properties props = new Properties();
props.load(fis);
for (String name : props.stringPropertyNames()) {
objectPool.put(name, createObject(props.getProperty(name)));
}
}
catch (IOException ex) {
Logger.getLogger(ObjectPoolFactory.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
if (fis != null) {
fis.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
//根据名字找对象
public Object getObject(String name){
return objectPool.get(name);
}
public static void main(String[] args) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
//a.properties
ObjectPoolFactory ob = new ObjectPoolFactory();
ob.initPool("D:/a.properties");
ob.getObject("a");
System.out.println(ob.getObject("a")+"=========");
}
}