读取配置文件

xiaoxiao2021-02-28  131

import java.util.Properties; import org.apache.log4j.LogManager;

import org.apache.log4j.Logger;

public class ConfigurationManager {

private static Logger log = LogManager.getLogger(ConfigurationManager.class);

private static Properties configProps = new Properties(); private static String CONFIG_FILE = "lmk.properties"; static { initialize(); } private static void initialize() { log.trace("Begin to intitialize ConfiguraitonManager..."); try { configProps.load(ConfigurationManager.class.getResourceAsStream(CONFIG_FILE)); String loginConfigFile = configProps.getProperty(ConfigurationManager.LOGIN_CONFIG); loginConfigFile = ConfigurationManager.class.getResource(loginConfigFile).getPath(); System.setProperty(ConfigurationManager.LOGIN_CONFIG,loginConfigFile); String waspLocation = configProps.getProperty(ConfigurationManager.WSAP_LOCATION); waspLocation = ConfigurationManager.class.getResource(waspLocation).getPath(); System.setProperty(ConfigurationManager.WSAP_LOCATION,waspLocation); } catch (Exception e) { log.error("Intitializing ConfiguraitonManager error: ", e); System.exit(0); } log.trace("Finish intitializing ConfiguraitonManager.");

}

/** * Get the configured value as integer. If not correctly configured, return 0. * @param configKey *            the key name * @return the int value */ public static String getConfigValue(String configKey) { String configValue = (String) configProps.getProperty(configKey); log.debug("ConfigurationManager.getConfiguration(" + configKey + ")"); log.debug("configValue=" + configValue); return configValue; } public static int getConfigValueAsInt(String configKey) { int intValue = 0; try { intValue = Integer.parseInt(getConfigValue(configKey)); } catch (NumberFormatException e) { intValue = 0; log.error("Incorrect int value configured for " + configKey); } return intValue; } public static boolean getConfigValueAsBoolean(String configKey){ boolean booleanValue = false; booleanValue = Boolean.parseBoolean(getConfigValue(configKey)); return booleanValue; } }
转载请注明原文地址: https://www.6miu.com/read-49374.html

最新回复(0)