1. 程式人生 > >讀取properties配置檔案中屬性的值的工具類

讀取properties配置檔案中屬性的值的工具類

自制讀取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; } }

大概就是如此~