@TOC

原始方式pro.load()与 pro.getProperty()配合的方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
构造器 
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);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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使用必须在使用的类必须能够扫描到

1
2
3
/** 模板编号(N) */
@Value("${unifiedability.mail.templateNum}")
private String templateNum;

application.yml

1
2
mail:
templateNum: 11111111111111111111#一串数字

@ConfigurationProperties(prefix = “前缀内容”)与@EnableConfigurationProperties({映射类.class})配合的方式

application.yml

1
2
3
4
5
baidu:
token:
APP_ID: ""
API_KEY: ""
SECRET_KEY: ""

映射实体类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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;
}

使用类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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));
}
}
}