那们兄台知道怎么从配置文件中加载类,然用用map中的key存类名,value存实例

解决方案 »

  1.   

    可以从配置文件中读取相关的类,然后利用Class.forName("..类");将其放入Map中
      

  2.   


    public Properties getProperty() {
    Properties pro = new Properties();
    InputStream in = null;
    try {
    in = getClass().getResourceAsStream("class.propertys");
    pro.load(in);
    } catch (Exception e) { } finally {
    if (in != null) {
    try {
    in.close();
    } catch (IOException e) {
    }
    }
    }
    return pro;
    } public void test() throws InstantiationException, IllegalAccessException, ClassNotFoundException {
    Map<String, Object> map = new HashMap<String, Object>();
    Properties  p = getProperty();
    Enumeration keys = p.keys();
    while(keys.hasMoreElements()) {
    String key = (String)keys.nextElement();
    String className = p.getProperty(key);
    Object obj = Class.forName(className).newInstance();
    map.put(key, obj);
    }
    }