获取Spring项目配置文件元素

 在开发中有时候要获取配置文件里的值,通常可以利用如下方式来读取:

public class PropertyUtil {
    
    private static Properties p = new Properties();
    
    static {
        init("config.properties");
    }
    
    /**
     * read config file and set vlaue to Properties p
     * @param propertyFileName 文件名
     */
    protected static void init(String propertyFileName)
    {
        InputStream in = null;
        try {
            in = PropertyUtil.class.getClassLoader().getResourceAsStream(propertyFileName);
            if (in != null){
                p.load(in);
            }
        } catch (IOException e){
            System.err.println("load ["+propertyFileName+"] error!"+e.getMessage());
        } finally {
            IOUtils.closeQuietly(in);
        }
    }
    
    public static String getValue(String key)
    {
        String value = null;
        try {
            value = p.getProperty(key);
        } catch (Exception e) {
            //
        }
        return value;
    }
}

下面推荐一个利用PropertyPlaceholderConfigurer来获取配置文件元素的方法:

package com.spring.common.util;

import java.util.HashMap;
import java.util.Map;import java.util.Properties;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

public final class PropertiesUtil extends PropertyPlaceholderConfigurer {
    
    private static Map<String, String> PropertiesMap;
    private static Properties properties = new Properties();
    
    @Override
    protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, 
            Properties props) throws BeansException {
        super.processProperties(beanFactoryToProcess, props);
        properties.putAll(props);
        PropertiesMap = new HashMap<String, String>();
        for (Object key : props.keySet()) {
            String keyStr = (String) key;
            String value = props.getProperty(keyStr);
            PropertiesMap.put(keyStr, value);
        }
    }

    /**
     * Get a value based on key , if key does not exist , null is returned
     * @param key
     * @return
     */
    public static String getString(String key) {
        try {
            return PropertiesMap.get(key);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 根据key获取值
     * 
     * @param key
     * @return
     */
    public static int getInt(String key) {
        return Integer.parseInt(PropertiesMap.get(key));
    }
    
    /**
     * 根据key获取值
     * 
     * @param key
     * @param defaultValue
     * @return
     */
    public static int getInt(String key, int defaultValue) {
        String value = PropertiesMap.get(key);
        if (StringUtils.isBlank(value)) {
            return defaultValue;
        }
        return Integer.parseInt(value);
    }
    
    public static boolean getBoolean(String key) {
        return getBoolean(key, false);
    }

    /**
     * 根据key获取值
     * @param key
     * @param defaultValue
     * @return
     */
    public static boolean getBoolean(String key, boolean defaultValue) {
        String value = PropertiesMap.get(key);
        if (StringUtils.isBlank(value)) {
            return defaultValue;
        }
        return Boolean.parseBoolean(value);
    }
    
}

需要在spring.xml文件里引入一下配置文件,注意类名全路径不要写错:

    <!-- 引入属性配置文件 -->
    <bean class="com.spring.common.util.PropertiesUtil">
        <property name="locations">
            <list>
                <value>classpath:config.properties</value>
                <value>classpath:jdbc.properties</value>
            </list>
        </property>
    </bean>

在需要的地方调用下getString方法:

String  password = PropertiesUtil.getString("password");
原文地址:https://www.cnblogs.com/yzuzhang/p/7399732.html