SpringBoot中獲取application.yml文件內容
原始方式pro.load()與 pro.getProperty()配合的方式
構造器
Properties pro=new Properties();
讀取配置文件的步驟 ★
a. pro加載配置文件
pro.load(InputStream in);
pro.load(Reader in);
b. 根據key值取value值
pro.getProperty(String key);根據key值取value值 如果沒有key值返回null
pro.getProperty(String key,String defaultvalue);根據key值取value值 如果沒有key值返回defaultvalue
設置鍵值對信息到配置文件
a. 設置鍵值對信息
pro.setProperty(String key, String value);
b. 應用到配置文件上
pro.store(OutputStream out, String commons);//comment是注釋的意思
pro.store(Writer out, String commons);
public class Demo5 {
public static void main(String[] args) {
//通過java代碼拿到配置文件中的信息
Properties pro=new Properties();
try {
//1. pro加載配置文件
pro.load(new FileInputStream("src\\db.properties"));
//2. 取值 根據key值取value值
String username = pro.getProperty("url1","123");
System.out.println(username);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
/*Properties pro=new Properties(); //1. 設置鍵值對數據 pro.setProperty("name", "john"); pro.setProperty("age", "18"); //2. 應用到配置文件上 try { pro.store(new FileOutputStream("src\\person.properties"), "person"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }*/
}
}
@Value注解方式
@Value使用必須在使用的類必須能夠掃描到
/** 模板編號(N) */
@Value("${unifiedability.mail.templateNum}")
private String templateNum;
application.yml
mail:
templateNum: 11111111111111111111#一串數字
@ConfigurationProperties(prefix = “前綴內容”)與@EnableConfigurationProperties({映射類.class})配合的方式
application.yml
baidu:
token:
APP_ID: ""
API_KEY: ""
SECRET_KEY: ""
映射實體類
package com.atguigu.demo.properties;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Repository;
@Repository
@ConfigurationProperties(prefix = "baidu.token")
@Data
public class BaiduProperties {
private String APP_ID;
private String API_KEY;
private String SECRET_KEY;
}
使用類
package com.atguigu.demo.service.impl;
import com.atguigu.demo.properties.BaiduProperties;
import com.atguigu.demo.service.BaiduSpeakService;
import com.atguigu.demo.vo.TextVo;
import com.baidu.aip.speech.AipSpeech;
import com.baidu.aip.speech.TtsResponse;
import com.baidu.aip.util.Util;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.HashMap;
@EnableConfigurationProperties({BaiduProperties.class})
@Service
public class BaiduSpeakServiceImpl implements BaiduSpeakService {
@Autowired
private BaiduProperties baiduProperties;
@Override
public void saveAudio(TextVo textVo) {
// 初始化一個AipSpeech
AipSpeech client = new AipSpeech(baiduProperties.getAPP_ID(), baiduProperties.getAPI_KEY(), baiduProperties.getSECRET_KEY());
// 可選:設置網絡連接參數
client.setConnectionTimeoutInMillis(2000);
client.setSocketTimeoutInMillis(60000);
//可選配置語速
HashMap<String, Object> options = new HashMap<String, Object>();
if(textVo.getSpd()!=null){
options.put("spd", textVo.getSpd());
}
if(textVo.getPit()!=null){
options.put("pit", textVo.getPit());
}
if(textVo.getPer()!=null){
options.put("per", textVo.getPer());
}
// 可選:設置代理服務器地址, http和socket二選一,或者均不設置
//client.setHttpProxy("proxy_host", proxy_port); // 設置http代理
//client.setSocketProxy("proxy_host", proxy_port); // 設置socket代理
// 可選:設置log4j日志輸出格式,若不設置,則使用默認配置
// 也可以直接通過jvm啟動參數設置此環境變量
//System.setProperty("aip.log4j.conf", "log4j.properties");
// 調用接口
TtsResponse res = client.synthesis(textVo.getText(), "zh", 1, options);
byte[] data = res.getData();
JSONObject res1 = res.getResult();
if (data != null) {
try {
Util.writeBytesToFileSystem(data, "D:/"+textVo.getName());
} catch (IOException e) {
e.printStackTrace();
}
}
if (res1 != null) {
System.out.println(res1.toString(2));
}
}
}