spring boot 中PropertiesUtil 工具类的创建和应用
package ;
import .;
import .Autowired;
import ;
import .Component;
import ;
import ;
/**
* Parsing The Configuration File
*
* @author ShenHuaJie
* @version 2016年7月30日 下午11:41:53
*/
@Component
public final class PropertiesUtil {
public static final byte[] KEY = { 9, -1, 0, 5, 39, 8, 6, 19 };
private static Environment env;
@Autowired
protected void set(Environment env) throws IOException {
PropertiesUtil.env = env;
}
/**
* 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 env.getProperty(key);
} catch (MissingResourceException e) {
return null;
}
}
/**
* Get a value based on key , if key does not exist , null is returned
*
* @param key
* @return
*/
public static String getString(String key, String defaultValue) {
try {
String value = env.getProperty(key);
if ((value)) {
return defaultValue;
}
return value;
} catch (MissingResourceException e) {
return defaultValue;
}
}
/**
* 根据key获取值
*
* @param key
* @return
*/
public static int getInt(String key) {
return (env.getProperty(key));
}
/**
* 根据key获取值
*
* @param key
* @param defaultValue
* @return
*/
public static int getInt(String key, int defaultValue) {
String value = env.getProperty(key);
if ((value)) {
return defaultValue;
}
return (value);
}
/**
* 根据key获取值
*
* @param key
* @param defaultValue
* @return
*/
public static boolean getBoolean(String key, boolean defaultValue) {
String value = env.getProperty(key);
if ((value)) {
return defaultValue;
}
return new Boolean(value);
}
}