读取配置文件

package com.csair.util;

import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Configuration
{
private static final Log log = LogFactory.getLog("common");
private static Properties props = null;
public static Properties getProperties()
{
if (props == null)
   initProperties();
   return props;
        }

public static String getProperty(String key)
{
   if (props == null)
     initProperties();
     return props.getProperty(key);
}

/**
*   读取应用程序的默认配置文件,并取得相关配置值
*/
private static synchronized void initProperties()
{
    props = new Properties();
   
    try
    {
    if (props.size() == 0) {   
input = Configuration.class.getClassLoader().getResourceAsStream("application.properties");
props.load(input);
}
    }
    catch(Exception ex)
    {
    props = null;    
    log.error(ex.getMessage());
    }
    finally
    {        
    if(input != null){
    try
    {
    input.close();
    input = null;
    }
    catch(Exception ex)
    {
    log.warn("close the application properties input stream failed:" + ex.getMessage());
    }
}
    }   
}
}

你可能感兴趣的:(读取配置文件)