首页 > 其他 > 详细

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

时间:2018-03-08 16:22:42      阅读:176      评论:0      收藏:0      [点我收藏+]

  首先在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;
    }
}

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

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

原文:https://www.cnblogs.com/sky-chen/p/8529075.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!