자바 프로필 읽 기 도구

4705 단어
1, 프로필 불 러 오기
public class PropertiesLoader {

    private static Logger logger = Logger.getLogger(PropertiesLoader.class);

    private static ResourceLoader resourceLoader = new DefaultResourceLoader();

    private final Properties properties;

    public PropertiesLoader(String... resourcesPaths) {
        properties = loadProperties(resourcesPaths);
    }

    public Properties getProperties() {
        return properties;
    }

    /**
     *   Property,  System Property  ,         .
     */
    private String getValue(String key) {
        String systemProperty = System.getProperty(key);
        if (systemProperty != null) {
            return systemProperty;
        }
        if (properties.containsKey(key)) {
            return properties.getProperty(key);
        }
        return "";
    }

    /**
     *   String   Property,  System Property  ,    Null     .
     */
    public String getProperty(String key) {
        String value = getValue(key);
        if (value == null) {
            throw new NoSuchElementException();
        }
        return value;
    }

    /**
     *   String   Property,  System Property  .    Null   Default .
     */
    public String getProperty(String key, String defaultValue) {
        String value = getValue(key);
        return value != null ? value : defaultValue;
    }

    /**
     *   Integer   Property,  System Property  .    Null          .
     */
    public Integer getInteger(String key) {
        String value = getValue(key);
        if (value == null) {
            throw new NoSuchElementException();
        }
        return Integer.valueOf(value);
    }

    /**
     *   Integer   Property,  System Property  .    Null   Default ,           
     */
    public Integer getInteger(String key, Integer defaultValue) {
        String value = getValue(key);
        return value != null ? Integer.valueOf(value) : defaultValue;
    }

    /**
     *   Double   Property,  System Property  .    Null          .
     */
    public Double getDouble(String key) {
        String value = getValue(key);
        if (value == null) {
            throw new NoSuchElementException();
        }
        return Double.valueOf(value);
    }

    /**
     *   Double   Property,  System Property  .    Null   Default ,           
     */
    public Double getDouble(String key, Integer defaultValue) {
        String value = getValue(key);
        return value != null ? Double.valueOf(value) : defaultValue;
    }

    /**
     *   Boolean   Property,  System Property  .    Null    ,      true/false   false.
     */
    public Boolean getBoolean(String key) {
        String value = getValue(key);
        if (value == null) {
            throw new NoSuchElementException();
        }
        return Boolean.valueOf(value);
    }

    /**
     *   Boolean   Property,  System Property  .    Null   Default ,      true/false   false.
     */
    public Boolean getBoolean(String key, boolean defaultValue) {
        String value = getValue(key);
        return value != null ? Boolean.valueOf(value) : defaultValue;
    }

    /**
     *       ,       Spring Resource  .
     */
    private Properties loadProperties(String... resourcesPaths) {
        Properties props = new Properties();
        for (String location : resourcesPaths) {
            InputStream is = null;
            try {
                Resource resource = resourceLoader.getResource(location);
                is = resource.getInputStream();
                props.load(is);
            } catch (IOException ex) {
                logger.info("Could not load properties from path:" + location + ", " + ex.getMessage());
            } finally {
                IOUtils.closeQuietly(is);
            }
        }
        return props;
    }

2. 설정 가 져 오기
public final class Global {

    private Global() {
    }

    public static Global getInstance() {
        return LazyHolder.GLOBAL;
    }

    private static class LazyHolder {
        private static final Global GLOBAL = new Global();
    }

    /**
     *        
     */
    private static Map map = Maps.newConcurrentMap();

    public static final String DEFAULT_CHARTSET = "UTF-8";


    /**
     *         
     */
    private static PropertiesLoader loader = new PropertiesLoader("      ");

    /**
     *     
     */
    public static String getConfig(String key) {
        String value = map.get(key);
        if (value == null) {
            value = loader.getProperty(key);
            map.put(key, value != null ? value : StringUtils.EMPTY);
        }
        return value;
    }
}

좋은 웹페이지 즐겨찾기