1. 程式人生 > >獲取Spring項目配置文件元素

獲取Spring項目配置文件元素

pre parse value row ack sage str lena ase

在開發中有時候要獲取配置文件裏的值,通常可以利用如下方式來讀取:

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");

獲取Spring項目配置文件元素