前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring配置文件的加载工具类

Spring配置文件的加载工具类

原创
作者头像
用户7999227
修改2021-09-24 10:42:25
7330
修改2021-09-24 10:42:25
举报
文章被收录于专栏:Java小王子Java小王子

Spring通用PropertiesUtil。Spring配置文件的加载工具类。

代码语言:javascript
复制
public class PropertiesUtil extends PropertyPlaceholderConfigurer implements
        Map<String, String> {

private static final Logger logger = Logger.getLogger(PropertiesUtil.class);
private static Map<String, String> ctxPropertiesMap;

protected void processProperties(
        ConfigurableListableBeanFactory beanFactoryToProcess,
        Properties props) throws BeansException {
    super.processProperties(beanFactoryToProcess, props);
    if (ctxPropertiesMap != null) {
        logger.warn("The property map will be override!");
    }
    ctxPropertiesMap = new HashMap<String, String>();
    for (Object key : props.keySet()) {
        String keyStr = key.toString();
        String value = props.getProperty(keyStr);
        ctxPropertiesMap.put(keyStr, value);
    }
}

public static String getString(String name) {
    if (ctxPropertiesMap == null) {
        ctxPropertiesMap = new HashMap<String, String>();
    }
    return (String) ctxPropertiesMap.get(name);
}

public static boolean getBoolean(String name, boolean defaultValue) {
    String v = getString(name);
    if (v == null) {
        return defaultValue;
    }
    try {
        return Boolean.parseBoolean(v);
    } catch (Exception e) {
    }
    return defaultValue;
}

public static int getIntValue(String name, int defaultValue) {
    String v = getString(name);
    if (v == null) {
        return defaultValue;
    }
    try {
        return Integer.parseInt(v);
    } catch (Exception e) {
    }
    return defaultValue;
}

public static long getLongValue(String name, long defaultValue) {
    String v = getString(name);
    if (v == null) {
        return defaultValue;
    }
    try {
        return Long.parseLong(v);
    } catch (Exception e) {
    }
    return defaultValue;
}

public static short getShortValue(String name, short defaultValue) {
    String v = getString(name);
    if (v == null) {
        return defaultValue;
    }
    try {
        return Short.parseShort(v);
    } catch (Exception e) {
    }
    return defaultValue;
}

public static double getDoubleValue(String name, double defaultValue) {
    String v = getString(name);
    if (v == null) {
        return defaultValue;
    }
    try {
        return Double.parseDouble(v);
    } catch (Exception e) {
    }
    return defaultValue;
}

public static float getFloatValue(String name, float defaultValue) {
    String v = getString(name);
    if (v == null) {
        return defaultValue;
    }
    try {
        return Float.parseFloat(v);
    } catch (Exception e) {
    }
    return defaultValue;
}

public int size() {
    return ctxPropertiesMap.size();
}

public boolean isEmpty() {
    return ctxPropertiesMap.isEmpty();
}

public boolean containsKey(Object key) {
    return ctxPropertiesMap.containsKey(key);
}

public boolean containsValue(Object value) {
    throw new UnsupportedOperationException();
}

public String put(String key, String value) {
    throw new UnsupportedOperationException();
}

public String remove(Object key) {
    throw new UnsupportedOperationException();
}

public void putAll(Map<? extends String, ? extends String> m) {
    throw new UnsupportedOperationException();
}

public void clear() {
    throw new UnsupportedOperationException();
}

public Set<String> keySet() {
    throw new UnsupportedOperationException();
}

public Collection<String> values() {
    throw new UnsupportedOperationException();
}

public Set<Map.Entry<String, String>> entrySet() {
    throw new UnsupportedOperationException();
}

public String get(Object key) {
    return (String) ctxPropertiesMap.get(key);
}

 
</pre> 

 使用方式,创建一个上面类的bean:
<bean id="propertyConfigurer"
        class="com.forg.common.PropertiesUtil">
        <property name="locations">
            <list>
                <value>classpath:system.properties</value>
            </list>
        </property>
    </bean>  

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
http://www.vxiaotou.com