1、类
public class ConfigData {
private Map<String, String> configs = null; private final String fileName = "config.xml"; private static ConfigData instance; private ConfigData() { } public static ConfigData getInstance() { if (instance == null) { instance = new ConfigData(); } return instance; } private void init() { configs = XmlResouceUtils.getAllElements(fileName); } public void addTags(String... tagNames) { if(tagNames==null||tagNames.length==0){ return; } String value = XmlResouceUtils.getXmlValue(fileName, tagNames); StringBuffer strBuf = new StringBuffer(); for (String str : tagNames) { strBuf.append(str + "_"); } strBuf.deleteCharAt(strBuf.length() - 1); configs.put(strBuf.toString(), value); } public String getValueByTag(String tagName) { String string = configs.get(tagName); return string; } public Map<String, String> getAll() { return this.configs; } } 2、访问方式public void testCofingData() throws Exception { Class<?> clazz = Class.forName("com.hoolai.basketball.resources.ConfigData"); Method method = clazz.getDeclaredMethod("init"); method.setAccessible(true); Field declaredField = clazz.getDeclaredField("configs"); declaredField.setAccessible(true); Constructor<?> constructor = clazz.getDeclaredConstructor(); constructor.setAccessible(true); Object instance = constructor.newInstance(); method.invoke(instance); Map<String, String> map = (Map<String, String>) declaredField.get(instance); printMap(map);//打印map }