路泰机电科技体检——数据平台后端
zjh
2024-12-24 cffd87559f2e9343ce3e5dcc5d2953272a244253
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
70
package com.example.config;
 
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
 
import javax.annotation.PostConstruct;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
 
/**
 * @Company: 西安路泰科技有限公司
 * @Author: zhaowenxuan
 * @Date: 2024/12/17 20:26
 */
@Slf4j
@Component
public class ConfigValue {
    @Value("${config.path}")
    private String CONFIG_PATH;
    @Value("${config.dir}")
    private String CONFIG_DIR;
    private Map<String, String> configMap = new HashMap<>();
 
    @PostConstruct
    private void init(){
        Properties props = new Properties();
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(CONFIG_PATH);
            props.load(fis);
            fis.close();
            for (String key : props.stringPropertyNames()) {
                configMap.put(key, props.getProperty(key));
            }
            log.info("配置 -> {}", JSONUtil.toJsonStr(configMap));
        } catch (IOException e) {
            throw new RuntimeException("找不到配置文件或加载异常");
        }
    }
 
    public Map<String, String> getConfigMap() {
        return configMap;
    }
 
    public String getConfigValue(String key) {
        return configMap.get(key);
    }
 
    public void refresh(){
        HashMap<String, String> hashMap = new HashMap<>();
        Properties props = new Properties();
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(CONFIG_PATH);
            props.load(fis);
            fis.close();
            for (String key : props.stringPropertyNames()) {
                hashMap.put(key, props.getProperty(key));
            }
            configMap = hashMap;
        } catch (IOException ignored) {
        }
    }
}