欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot加載一個properties文件轉換為map方式

 更新時間:2022年07月08日 10:13:49   作者:changerzhuo  
這篇文章主要介紹了springboot加載一個properties文件轉換為map方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

加載一個properties文件轉換為map

springboot中比較常見的獲取properties中的值,就是直接在字段上面添加@Value的屬性. 

但有時候我們不確定key有多少,但是會有一定的規(guī)律(這個規(guī)律是根據(jù)業(yè)務來定的,如下),這時候我們就可以考慮將properties中的信息轉換為一個map,然后根據(jù)key的規(guī)律操作響應的數(shù)據(jù)

1.創(chuàng)建一個properties文件

properties文件內(nèi)容

# 配置格式嚴格按照如下設置; app1, app2為具體的模塊名稱
wechat.data.app1.appId=appid1
wechat.data.app1.secret=secret1
?
wechat.data.app2.appId=appid2
wechat.data.app2.secret=
?
wechat.data.app3.appId=appid3
wechat.data.app3.secret=secret3

2.在java中將該properties文件轉換為map

package com.bz.wes.login.loginapi.config; 
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component; 
import java.util.HashMap;
import java.util.Map;
 
/**
 * @author changez
 * @desc
 * @datetime 2019/9/25 17:06
 */
@Data
@Component
// 指定配置文件
@PropertySource("classpath:wechatInfo.properties")
@ConfigurationProperties(prefix = "wechat")
public class WechatPropertiesConfig {
 
    // prefix的值+data變量名為properties key的前一部分, 將key剩余的部分作為map的key, value作為map的value
    public Map<String, String> data = new HashMap();
}

properties配置文件出現(xiàn)亂碼

如果使用properties作為配置文件

那么會出現(xiàn)字符亂碼,因為idea默認字符集是utf-8,properties默認是使用ascll碼,所以需要進行轉換

settings中搜索file encoidings,進行如下操作

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

最新評論