.properties文件下的參數值替換

今天小編簡單記錄下如何使用java提供的API來對字符串消息進行替換或者匹配操做,可是,這並不是正則表達式。java

平常開發中,咱們常常將一些配置信息記錄到諸如.properties文件當中,舉個小栗子,當咱們在開發微信公衆號時,咱們就會想着把某一個微信api接口路徑url配置到wechat.properties中,下面咱們就假設該url爲:正則表達式

https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_codeapi

這是微信獲取access_token的接口路徑,很明顯,它須要咱們傳入4個參數,分別是appidsecretcodegrant_type,因爲參數值是可變的,因此一般咱們不會像下面這樣直接將整個url地址配置到wechat.properties文件當中,錯誤的示範以下:bash

wechat.properties微信

wechatOAuthTokenUrl=https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
複製代碼

若是咱們在程序中提取wechatOAuthTokenUrl變量的值,那麼對參數的賦值操做將變成一件很是棘手的事情。一般的作法是咱們只配置url地址的前一小部分,即至關長時間不會改變的部分,即:app

wechatOAuthTokenUrl=https://api.weixin.qq.com/sns/oauth2/access_tokenasync

而後再程序中,當咱們想要發送請求的時候,只須要將4個參數構形成一個map便可發起請求,示例以下:學習

public static void main(String[] args) throws IOException {
    // 讀取屬性文件,通常不會這麼作,集成Spring時可作配置
    Properties properties = new Properties();
    InputStream inputStream = new FileInputStream("src/main/resources/wechat.properties");
    properties.load(inputStream);
    
    String wechatOAuthTokenUrl = properties.getProperty("wechatOAuthTokenUrl");
    
    // 參數map
    Map<String, String> map = new HashMap<>();
    map.put("appid", "A");
    map.put("secret", "B");
    map.put("code", "C");
    map.put("grant_type", "D");
    
    // 請求參數,封裝了url、參數、header等信息
    RequestParam param = new RequestParam();
    param.setUrl(wechatOAuthTokenUrl);
    param.setBody(map);
    // 發送請求
    HttpClientUtil.getInstance().asyncSend(param);
}
複製代碼

那麼還有沒有其餘方式呢?額,有,下面的這種方式採用佔位符來完成參數的匹配,佔位符匹配不是什麼高大上的東西,原則上採用正則表達式也能夠完成,可是沒有必要採用正則表達式。測試

一、採用java.text包下的MessageFormat

採用該類,咱們只須要將屬性配置成以下格式便可: wechatOAuthTokenUrl=https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type={3}url

測試類以下:

public class PropertiesFormater {
    public static void main(String[] args) throws IOException {
        Properties properties = new Properties();
        InputStream inputStream = new FileInputStream("src/main/resources/wechat.properties");
        properties.load(inputStream);
        
        // 先打印出未替換的wechatOAuthTokenUrl屬性值
        String wechatOAuthTokenUrl = properties.getProperty("wechatOAuthTokenUrl");
        System.err.println(wechatOAuthTokenUrl);
        
        // 使用MessageFormat對字符串進行參數替換
        wechatOAuthTokenUrl = MessageFormat.format(wechatOAuthTokenUrl,
        		new Object[] { "PPID", "SECRET", "CODE", "authorization_code" });
        System.err.println(wechatOAuthTokenUrl);
    }
}
複製代碼

輸出以下:

https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type={3}
https://api.weixin.qq.com/sns/oauth2/access_token?appid=PPID&secret=SECRET&code=CODE&grant_type=authorization_code
複製代碼
二、採用String.format()進行匹配

url屬性格式配置以下: wechatOAuthTokenUrl=https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=%s

測試類以下:

public class PropertiesFormater {
    public static void main(String[] args) throws IOException {
        Properties properties = new Properties();
        InputStream inputStream = new FileInputStream("src/main/resources/wechat.properties");
        properties.load(inputStream);
        
        // 先打印出未替換的wechatOAuthTokenUrl屬性值
        String wechatOAuthTokenUrl = properties.getProperty("wechatOAuthTokenUrl");
        System.err.println(wechatOAuthTokenUrl);
        
        wechatOAuthTokenUrl = String.format(wechatOAuthTokenUrl, new Object[] { "A", "B", "C", "D" });
        System.err.println(wechatOAuthTokenUrl);
    }
}
複製代碼

輸出以下:

https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=%s
https://api.weixin.qq.com/sns/oauth2/access_token?appid=A&secret=B&code=C&grant_type=D
複製代碼

能夠看出第一種方式採用大括號進行匹配,第二種方式採用像C語言那樣的%s匹配,並且,本文只是簡單演示了匹配的一種狀況,足以解決咱們的需求,String的佔位符匹配還有不少,你們請各自深刻學習之,謝謝閱讀!

相關文章
相關標籤/搜索