调用:
Configure.getValue("discount.strategy.class");
配置类:
package com.util; import com.sun.javafx.fxml.PropertyNotFoundException; import java.io.InputStream; import java.util.Properties; public class Configure { private static Properties config; static { System.out.println("初始化加载配置!"); String filePath = "application.properties"; config = new Properties(); try { ClassLoader CL = Configure.class.getClassLoader(); InputStream in; if (CL != null) { in = CL.getResourceAsStream(filePath); } else { in = ClassLoader.getSystemResourceAsStream(filePath); } config.load(in); in.close(); } catch (Exception e) { throw new PropertyNotFoundException("服务器配置信息读取错误:" + e.getMessage()); } } public static String getValue(String key) { if (config.containsKey(key)) { String value = config.getProperty(key); return value; } else { return ""; } } public static int getValueInt(String key) { String value = getValue(key); int valueInt = 0; try { valueInt = Integer.parseInt(value); } catch (NumberFormatException e) { e.printStackTrace(); return valueInt; } return valueInt; } }