加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Spring配置文件的加载工具类

发布时间:2020-12-14 23:38:36 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 Spring通用PropertiesUtil。Spring配置文件的加载工具类。 public class PropertiesUtil extends PropertyPlaceholderConfigurer implements MapStrin

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

Spring通用PropertiesUtil。Spring配置文件的加载工具类。
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);
    }


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

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读