首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >配置文件properties读取使用的好方法

配置文件properties读取使用的好方法

作者头像
老梁
发布2019-09-10 16:41:07
6070
发布2019-09-10 16:41:07
举报

  首先在spring配置文件applicationContext.xml中配置、

    <bean id="placeholderConfig"
          class="com.beikbank.common.utils.PropertyConfigurer">
        <property name="locations">
            <list>
                <value>classpath:jdbc.properties</value>
                <value>classpath:redis.properties</value>
                <value>classpath:SysConfig.properties</value>
            </list>
        </property>
    </bean>

如果我们要在代码中使用SysConfig.properties中配置信息,我们可以自己写个类PropertyConfigurer继承PropertyPlaceholderConfigurer·

public class PropertyConfigurer extends PropertyPlaceholderConfigurer {

    private static Map<String, String> propertyMap;

    @Override
    protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException {
        super.processProperties(beanFactoryToProcess, props);
        propertyMap = new HashMap<>();
        for (Object key : props.keySet()) {
            String keyStr = key.toString();
            String value = props.getProperty(keyStr);
            propertyMap.put(keyStr, value);
        }
    }

    public static String getProperty(String name) {
        return propertyMap.get(name);
    }

    /**
     * 返回propertyMap
     *
     * @return
     */
    public static Map<String, String> getPropertyMap() {
        return propertyMap;
    }
}

这样把属性值在系统启动的时候就设进去可以后续直接调用静态方法了

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-03-08 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

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

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

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