讀取yaml 的配置文件
配置文件信息
iot_saas_tenement:user_id: 7........8d9bprivate_key: MII.......qQ==bj_url: http://4.....5:8088project_name: iot_s.......rojectdevice_name: te.....ice

創建一個類?ProxyProperties 讀取配置文件信息,并對外提供get方法
package com.purvardata.himp.third.bj.utils;import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;// 獲取yaml的配置信息添加到靜態方法
@Component
public final class ProxyProperties {@Value("${iot_saas_tenement.bj_url}")private String bj_url;private static String url;@Value("${iot_saas_tenement.user_id}")private String user_id;private static String userId;@Value("${iot_saas_tenement.private_key}")private String private_key;private static String privateKey;@Value("${iot_saas_tenement.project_name}")private String project_name;private static String projectName;@Value("${iot_saas_tenement.device_name}")private String device_name;private static String deviceName;@PostConstructpublic void setUrl() {url=this.bj_url;userId=this.user_id;privateKey=this.private_key;projectName=this.project_name;deviceName=this.device_name;}public static String getUrl() {return url;}public static String getUserId() {return userId;}public static String getPrivateKey() {return privateKey;}public static String getProjectName() {return projectName;}public static String getDeviceName() {return deviceName;}
}
目標靜態方法通過get方法獲取對應的屬性

通過類 ResourceBundle 讀取 config.properties 的配置文件
config.properties配置文件信息
userId=7dd.......9b
private_key=MIIC........Q==
url=http://4......5:8088
project_name=iot_sa..............ect

定義讀取 配置類?PropertiesUtils,注意 config.properties 目錄,要是和 ResourceBundle.getBundle("config")路徑一致,我這里放根路徑了
package com.iline.bj;import java.util.ResourceBundle;public class PropertiesUtils {private static ResourceBundle bundle = ResourceBundle.getBundle("config");/*** 獲取值** @param key* @return*/public static String getValue(String key) {return bundle.getString(key);}}
使用配置類 PropertiesUtils.getValue 獲取配置文件 config.properties? 的信息
