在SpringBoot项目中怎样引入.yml文件中的设置
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));
}
}
}
在SpringBoot项目中怎样引入.yml文件中的设置的更多相关文章
- 你有没有觉得邮件发送人固定配置在yml文件中是不妥当的呢?SpringBoot 动态设置邮件发送人
明月当天,不知道你有没有思念的人 前言 之前其实已经写过SpringBoot异步发送邮件,但是今天在一个小项目中要用到发送邮件时,我突然觉得邮件发送人只有一个,并且固定写在yml文件中,就是非常的不妥 ...
- Springboot读取自定义的yml文件中的List对象
Yml文件(novellist.xml)如下: novellist: list: - name: 笑傲江湖 type: 武侠 master: 令狐冲 a ...
- 使用外部容器运行spring-boot项目:不使用spring-boot内置容器让spring-boot项目运行在外部tomcat容器中
前言:本项目基于maven构建 spring-boot项目可以快速构建web应用,其内置的tomcat容器也十分方便我们的测试运行: spring-boot项目需要部署在外部容器中的时候,spring ...
- JAVA - SpringBoot项目引用generator生成 Mybatis文件
JAVA - SpringBoot项目引用generator生成 Mybatis文件 在spring官网https://start.spring.io/自动生成springboot项目,这里选择项目 ...
- Spring boot中普通工具类不能使用@Value注入yml文件中的自定义参数的问题
在写一个工具类的时候,因为要用到yml中的自定义参数,使用@Value发现值不能正常注入,都显示为null: yml文件中的自定义格式 调用工具类的时候不能new的方式 要使用@Autowired的方 ...
- Bukkit编程之动态向yml文件中添加属性
yaml = new Yaml(); String goods = args[0]; String goodsNum = args[1]; YamlConfiguration yc = new Yam ...
- Pycharm中 import 引入同级文件失败问题
Pycharm中 import 引入同级文件失败,如下所示: “This inspection detects names that should resolve but don't. Due to ...
- Vue在单独引入js文件中使用ElementUI的组件
Vue在单独引入js文件中使用ElementUI的组件 问题场景: 我想在vue中的js文件中使用elementUI中的组件,因为我在main.js中引入了element包和它的css,并挂载到了全局 ...
- springboot2.0application.在yml文件中添加自定义配置
1. 在application.yml文件中添加自定义配置 app: platform: version: code: '1.0.0' 2. 定义bean类 具体格式: 其中的成员变量名称需要与配 ...
随机推荐
- 【问题】【SpringBoot】记一次springboot框架下用jackson解析RequestBody失败的问题
最近项目中遇到了一个问题,费好大劲才发现问题所在,并且修复了问题,下面分享一下这个问题的定位和修复的新路旅程. 先说下背景:该项目用的是SpringBoot框架,主要功能为对外提供一些Restful ...
- 03 父子组件sync&update
父组件传给子组件是基本数据类型. 父组件 <template> <el-container class="consele-container"> <e ...
- Python 零基础快速入门!
“人生苦短,我学python”是编程届的名言.用python写小脚本的便捷性,让很多其他语言的学习者把python当作辅助语言.拥有了某一个语言的功底,再来学习另外一种语言应该是十分快速的.编程理念都 ...
- 查看Java一段程序运行了多长时间(以几小时几分几秒的形式显示)
我们通常可以用 long ms=System.currentTimeMillis(); 来取得以毫秒为单位起始时间和终止时间,它们的时间差除以一千就知道一段Java程序运行了多少秒,但多少秒并不直观, ...
- 10 张图聊聊线程的生命周期和常用 APIs
上一篇文章我们聊了多线程的基础内容,比如为什么要使用多线程,线程和进程之间的不同,以及创建线程的 4 种方式.本文已收录至我的 Github: https://github.com/xiaoqi666 ...
- Odoo10中calendar视图点击事件
有个需求,需要根据该条记录的状态字段来控制点击calendar时是否需要打开form视图,解决方案如下:重写了web_calendar的get_fc_init_options()方法中的eventCl ...
- PHP木马免杀的一些总结
前言 这篇文章写一些php木马免杀的一些技巧,希望对大家有点帮助.这里解释一下什么是php木马,这里大体分为三种: 能完成写入文件.列目录.查看文件.执行一些系统命令等少量功能的,这种的是" ...
- python文件的读写权限以及相关应用read、write和文件指针
f=open('ceshi.txt','a',encoding='utf-8')r=open('ceshi.txt','r',encoding='utf-8')上面的2种写法可以用with来写:wit ...
- ulimit 的认识
原文出自 通过 ulimit 改善系统性能 概述 系统性能一直是一个受关注的话题,如何通过最简单的设置来实现最有效的性能调优,如何在有限资源的条件下保证程序的运作,ulimit 是我们在处理这些问题时 ...
- Linux环境变量总结 转
转自https://www.jianshu.com/p/ac2bc0ad3d74 Linux是一个多用户多任务的操作系统,可以在Linux中为不同的用户设置不同的运行环境,具体做法是设置不同用户的环境 ...