PropertiesLoader.java 1.9 KB
package com.framework.util;


import java.io.InputStream;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * @description roperties属性文件加载
 * @author gerry.zhang
 * @date 2014-5-29
 * @version 1.0
 *
 */
public class PropertiesLoader {
	
	private static final Logger logger = LoggerFactory.getLogger(PropertiesLoader.class); 
	
	private static final String CONFIG_FILE = "/config.properties";

	private static final String REDIS_FILE = "/redis.properties";

	private static final String LOGIN_FILE = "/openRemoteLogin.properties";
	
	/**
	 * 配置文件properties获取value
	 * @param key
	 * @param propertyFile 注意classpath下文件以"/"开始
	 * @return
	 */
	public static String get(String key){
		Properties properties = loadProperty("/application.properties");
		return (String) properties.get(key);
	}
	
	
	/**
	 * load properties文件
	 * @param propertyFile
	 * @return
	 */
	public static Properties loadProperty(String propertyFile){
		InputStream in=PropertiesLoader.class.getResourceAsStream(propertyFile); 
		Properties properties = new Properties();
		try{
			properties.load(in);
		}catch(Exception e){
			logger.error("解析文件失败:文件名={}", propertyFile, e);
		}
		return properties;
	}

	public static String getRedis(String key) {
		Properties properties = PropertiesLoader.loadProperty(REDIS_FILE);
		return properties.getProperty(key);
	}

	public static String getLogin(String key) {
		Properties properties = PropertiesLoader.loadProperty(LOGIN_FILE);
		return properties.getProperty(key);
	}
	
	
	public static String getConfig(String key) {
		Properties properties = PropertiesLoader.loadProperty(CONFIG_FILE);
		return properties.getProperty(key);
	}


	public static String get(String key,String propertyFile){
		Properties properties = loadProperty(propertyFile);
		return (String) properties.get(key);
	}
	
	
	
	 

}