版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/System_out_print_Boy/article/details/80522831
自制读取properties配置文件的工具类
项目中常用的一些配置的维护,例如redis的ip,数据库ip这些一点是用文件维护起来,方便项目开发时进行调试,为了方便以后使用,就直接封装了一个工具类以便使用,代码如下:
/**
* 读取properties配置文件中属性的值的工具类
* @author zzx
*/
public class ReadPropertiesUtils {
public static void main(String[] args) {
String SHOPXX_PROPERTIES_PATH = "classpath:shopxx-url.properties";
String resourceName = "url.app_server_ip";
String aa = getAttrValue(SHOPXX_PROPERTIES_PATH, resourceName);
System.out.println(aa);
}
/**
* 读取properties配置文件中属性的值
* @param resourceLocation 文件名 比如:classpath:shopxx.properties
* @param resourceName 资源名
* @return
*/
public static String getAttrValue(String resourceLocation, String resourceName){
Properties properties = null;
try {
File shopxxPropertiesFile = ResourceUtils.getFile(resourceLocation);
properties = PropertiesLoaderUtils.loadProperties(new FileSystemResource(shopxxPropertiesFile));
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
String attributeValue = properties.getProperty(resourceName);
return attributeValue;
}
}
大概就是如此~