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