首页 技术 正文
技术 2022年11月9日
0 收藏 500 点赞 3,156 浏览 3767 个字

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));
}
}
}

原文引自

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,488
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289