构建自己的commons
web项目中加载配置信息,废话不说,直接上代码
?
ClassLoader loader = Thread.currentThread().getContextClassLoader();URL url = null;String propertyName = "url.properties";if (loader instanceof URLClassLoader) {URLClassLoader ucl = (URLClassLoader) loader;url = ucl.findResource(propertyName);}if (url == null) {url = loader.getResource(propertyName);}if (url == null) {System.out.println("No properties file: " + propertyName + " found");} else {Properties bundle = new Properties();InputStream is = url.openStream();if (is != null) {bundle.load(is);is.close();} else {throw new IOException("Properties file " + propertyName + " not available");}Enumeration<?> propertyNames = bundle.propertyNames();while (propertyNames.hasMoreElements()) {String key = (String) propertyNames.nextElement();String value = bundle.getProperty(key);groupListMap.put(key, Arrays.asList(value.split(",")));System.out.print("Loaded user " + key + ":");for (String role : groupListMap.get(key)) {System.out.print(" " + role);}System.out.println();}}?