作者 申海龙

将代码中的reids 写到配置文件中

package com.agent.util;
import com.framework.util.PropertiesLoader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.InputStream;
import java.util.Properties;
/**
* @Auther: shenhl
* @Date: 2019/3/6 15:22
*/
public class RedisUtils {
private static final Logger logger = LoggerFactory.getLogger(RedisUtils.class);
private static final String CONFIG_FILE = "/config.properties";
/**
* 配置文件properties获取value
* @param key
* @param propertyFile 注意classpath下文件以"/"开始
* @return
*/
public static String get(String key){
Properties properties = loadProperty("/redis.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 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);
}
}
... ...
host = 10.50.3.84
port = 6379
\ No newline at end of file
... ...