0%

从yml配置读取数据工具类

不知道为什么,有时候@Value注解读取不到配置文件的数据,所以搞了一个工具类来读取。

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
/**
* @Description:配置类-从yml配置中获取数据
* @Author: zhanglang
* @Date: 2021/06/27 14:24
*/
public class ymlUtil {
public static String MQTT_HOST;
public static String MQTT_CLIENTID;
public static String MQTT_USER_NAME;
public static String MQTT_PASSWORD;
public static Integer MQTT_TIMEOUT;
public static Integer MQTT_KEEP_ALIVE;

static {
MQTT_HOST = loadMqttYml().getProperty("host");
MQTT_CLIENTID = loadMqttYml().getProperty("clientId");
MQTT_USER_NAME = loadMqttYml().getProperty("username");
MQTT_PASSWORD = loadMqttYml().getProperty("password");
MQTT_TIMEOUT = Integer.valueOf(loadMqttYml().getProperty("timeout"));
MQTT_KEEP_ALIVE = Integer.valueOf(loadMqttYml().getProperty("keepalive"));
}

/**
* 从yml从获取数据
* @return
*/
private static Properties loadMqttYml() {
InputStream inputstream = ymlUtil.class.getResourceAsStream("/application.yml");
Properties properties = new Properties();
try {
properties.load(inputstream);
return properties;
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
try {
if (inputstream != null) {
inputstream.close();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

}
----------本文结束感谢您的阅读----------