.properties文件下的参数值替换

1,763 阅读3分钟

今天小编简单记录下如何使用java提供的API来对字符串消息进行替换或者匹配操作,但是,这并非正则表达式。

日常开发中,我们经常将一些配置信息记录到诸如.properties文件当中,举个小栗子,当我们在开发微信公众号时,我们就会想着把某一个微信api接口路径url配置到wechat.properties中,下面我们就假设该url为:

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

这是微信获取access_token的接口路径,很明显,它需要我们传入4个参数,分别是appidsecretcodegrant_type,由于参数值是可变的,所以通常我们不会像下面这样直接将整个url地址配置到wechat.properties文件当中,错误的示范如下:

wechat.properties

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

如果我们在程序中提取wechatOAuthTokenUrl变量的值,那么对参数的赋值操作将变成一件非常棘手的事情。通常的做法是我们只配置url地址的前一小部分,即相当长时间不会改变的部分,即:

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

然后再程序中,当我们想要发送请求的时候,只需要将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);
}

那么还有没有其他方式呢?额,有,下面的这种方式采用占位符来完成参数的匹配,占位符匹配不是什么高大上的东西,原则上采用正则表达式也可以完成,但是没有必要采用正则表达式。

1、采用java.text包下的MessageFormat

采用该类,我们只需要将属性配置成如下格式即可: wechatOAuthTokenUrl=https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type={3}

测试类如下:

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
2、采用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的占位符匹配还有很多,大家请各自深入学习之,谢谢阅读!